Module: PrettyYaml
Constant Summary collapse
- INDENT =
' '
Instance Method Summary collapse
-
#empty_value(value) ⇒ Object
Formats an empty_value.
-
#format_key(key) ⇒ Object
Formats a data key.
-
#format_value(value) ⇒ Object
Formats a data value.
-
#indent_string(level = 0) ⇒ Object
Creates space indentation for a string, based on a level int.
-
#print(data, level = 0) ⇒ Object
Prints the actual data.
Instance Method Details
#empty_value(value) ⇒ Object
Formats an empty_value.
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ppy.rb', line 51 def empty_value(value) case value when Array format_value('[ ]') when Hash format_value('{ }') else value end end |
#format_key(key) ⇒ Object
Formats a data key.
63 64 65 |
# File 'lib/ppy.rb', line 63 def format_key(key) key.to_s.color(:yellow) end |
#format_value(value) ⇒ Object
Formats a data value.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ppy.rb', line 68 def format_value(value) case value when Numeric value.to_s.color(:cyan) when String value when TrueClass value.to_s.color(:green) when FalseClass value.to_s.color(:red) when NilClass 'nil'.color(:blue) else value end end |
#indent_string(level = 0) ⇒ Object
Creates space indentation for a string, based on a level int.
46 47 48 |
# File 'lib/ppy.rb', line 46 def indent_string(level = 0) INDENT * level end |
#print(data, level = 0) ⇒ Object
Prints the actual data.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ppy.rb', line 9 def print(data, level = 0) case data when Hash data.each do |key, value| if value.is_a? Enumerable string = "#{indent_string(level)}#{format_key(key)}:" if value.empty? puts string + ' ' + empty_value(value) else puts string print(value, level + 1) end else puts "#{indent_string(level)}#{format_key(key)}: #{format_value(value)}" end end when Array data.each do |value| if value.is_a? Enumerable string = format_key "#{indent_string(level)}-" if value.empty? puts string + ' ' + empty_value(value) else puts string print(value, level + 1) end else puts "#{indent_string(level)}#{format_key('-')} #{format_value(value)}" end end else puts "#{indent_string(level)}#{format_value(data)}" end end |