Class: IParty::CLI::Formatter::StringFormatter
Constant Summary
VOID_OUTPUT
Constants included
from Colorize
Colorize::COLORMAP
Instance Method Summary
collapse
#colorize?, descendants, find_by_id, #initialize
Methods included from Colorize
#colorize, #decolorize, #with_color
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
|
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
|
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
|