class AwesomePrint::Formatters::HashFormatter
Attributes
hash[R]
inspector[R]
options[R]
Public Class Methods
new(hash, inspector)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 8 def initialize(hash, inspector) @hash = hash @inspector = inspector @options = inspector.options end
Public Instance Methods
format()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 14 def format if hash.empty? empty_hash elsif multiline_hash? multiline_hash else simple_hash end end
Private Instance Methods
empty_hash()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 26 def empty_hash '{}' end
left_width(keys)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 59 def left_width(keys) result = max_key_width(keys) result += indentation if options[:indent] > 0 result end
max_key_width(keys)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 65 def max_key_width(keys) keys.map { |key, _value| key.size }.max || 0 end
multiline_hash()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 34 def multiline_hash ["{\n", printable_hash.join(",\n"), "\n#{outdent}}"].join end
multiline_hash?()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 30 def multiline_hash? options[:multiline] end
plain_single_line() { || ... }
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 94 def plain_single_line plain = options[:plain] multiline = options[:multiline] options[:plain] = true options[:multiline] = false yield ensure options[:plain] = plain options[:multiline] = multiline end
pre_ruby19_syntax(key, value, width)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 90 def pre_ruby19_syntax(key, value, width) align(key, width) << colorize(' => ', :hash) << inspector.awesome(value) end
printable_hash()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 42 def printable_hash data = printable_keys width = left_width(data) data.map! do |key, value| indented do if options[:ruby19_syntax] && symbol?(key) ruby19_syntax(key, value, width) else pre_ruby19_syntax(key, value, width) end end end should_be_limited? ? limited(data, width, hash: true) : data end
printable_keys()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 69 def printable_keys keys = hash.keys keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys] keys.map! do |key| plain_single_line do [String.new(inspector.awesome(key)), hash[key]] end end end
ruby19_syntax(key, value, width)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 85 def ruby19_syntax(key, value, width) key[0] = '' align(key, width - 1) << colorize(': ', :hash) << inspector.awesome(value) end
simple_hash()
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 38 def simple_hash "{ #{printable_hash.join(', ')} }" end
symbol?(key)
click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 81 def symbol?(key) key[0] == ':' end