Class: IParty::CLI::Formatter::StringFormatter

Inherits:
IParty::CLI::Formatter show all
Defined in:
lib/iparty/cli/formatter.rb

Constant Summary

Constants inherited from IParty::CLI::Formatter

VOID_OUTPUT

Constants included from Colorize

Colorize::COLORMAP

Instance Method Summary collapse

Methods inherited from IParty::CLI::Formatter

#colorize?, descendants, find_by_id, #initialize

Methods included from Colorize

#colorize, #decolorize, #with_color

Constructor Details

This class inherits a constructor from IParty::CLI::Formatter

Instance Method Details

#dig_loose_value(data, key, locals = {}) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/iparty/cli/formatter.rb', line 151

def dig_loose_value data, key, locals = {}
  key_part, def_val = key.to_s.split("|", 2)
  chunks = key_part.split(".").map{ _1.match?(/\A-?\d+\z/) ? _1.to_i : _1.to_sym }

  return locals.fetch(chunks[0]) if chunks.length == 1 && locals.key?(chunks[0])

  dig_loose_value_in(data, chunks) || def_val
end

#dig_loose_value_in(ptr, chunks) ⇒ Object



160
161
162
163
164
165
# File 'lib/iparty/cli/formatter.rb', line 160

def dig_loose_value_in ptr, chunks
  return ptr if chunks.empty?
  return ptr unless ptr.is_a?(Hash) || ptr.is_a?(Array)

  dig_loose_value_in(ptr[chunks[0]], chunks[1..])
end

#format(ip, index: 0, **kw, &to_data) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/iparty/cli/formatter.rb', line 140

def format ip, index: 0, **kw, &to_data
  data = to_data.call(ip)
  locals = { 1 => ip }
  fwd_hash = Hash.new do |_, key|
    dig_loose_value(data, key, locals) || (@app.opts[:debug] ? nil : "")
  end

  out = @opts[:argument] % fwd_hash
  out.empty? ? VOID_OUTPUT : out
end

#format_all(ips, base_index: 0, **kw, &to_data) ⇒ Object



136
137
138
# File 'lib/iparty/cli/formatter.rb', line 136

def format_all ips, base_index: 0, **kw, &to_data
  ips.map.with_index {|ip, index| format(ip, index: base_index + index, **kw, &to_data) }
end