Class: JsonColorizer
- Inherits:
-
Object
- Object
- JsonColorizer
- Defined in:
- lib/json-colorizer.rb
Instance Method Summary collapse
- #apply_formatters(data, formatters) ⇒ Object
- #colorize(s, color_options) ⇒ Object
- #format(data) ⇒ Object
-
#initialize(schema) ⇒ JsonColorizer
constructor
A new instance of JsonColorizer.
Constructor Details
#initialize(schema) ⇒ JsonColorizer
Returns a new instance of JsonColorizer.
4 5 6 7 8 |
# File 'lib/json-colorizer.rb', line 4 def initialize(schema) @schema = schema @schema_keys = @schema.keys @other = schema.delete(:_other) end |
Instance Method Details
#apply_formatters(data, formatters) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/json-colorizer.rb', line 24 def apply_formatters(data, formatters) return '' if data.nil? formatters.reduce(data) do |output, formatter| case formatter when Proc formatter.call(output) when Symbol self.send(formatter, output) else output end end end |
#colorize(s, color_options) ⇒ Object
51 52 53 |
# File 'lib/json-colorizer.rb', line 51 def colorize(s, ) s.to_s.colorize() end |
#format(data) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/json-colorizer.rb', line 10 def format(data) parts = [] @schema.each do |key, formatters| formatters = [formatters] unless formatters.is_a?(Array) parts << apply_formatters(data.fetch(key, data.fetch(key.to_s, nil)), formatters) end unless @other.nil? parts << data.select{ |k, v| !@schema_keys.include?(k) }.to_s end parts.join(' ') end |