class AmazingPrint::Formatters::HashFormatter
Attributes
hash[R]
inspector[R]
options[R]
Public Class Methods
new(hash, inspector)
click to toggle source
Calls superclass method
# File lib/amazing_print/formatters/hash_formatter.rb, line 10 def initialize(hash, inspector) super() @hash = hash @inspector = inspector @options = inspector.options end
Public Instance Methods
format()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 17 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/amazing_print/formatters/hash_formatter.rb, line 29 def empty_hash '{}' end
key_size(key)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 68 def key_size(key) return key.inspect.size if symbol?(key) if options[:html] single_line { inspector.awesome(key) }.size else plain_single_line { inspector.awesome(key) }.size end end
left_width(keys)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 62 def left_width(keys) result = max_key_width(keys) result += indentation if options[:indent].positive? result end
max_key_width(keys)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 78 def max_key_width(keys) keys.map { |key, _value| key_size(key) }.max || 0 end
multiline_hash()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 37 def multiline_hash ["{\n", printable_hash.join(",\n"), "\n#{outdent}}"].join end
multiline_hash?()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 33 def multiline_hash? options[:multiline] end
plain_single_line(&block)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 110 def plain_single_line(&block) plain = options[:plain] options[:plain] = true single_line(&block) ensure options[:plain] = plain end
pre_ruby19_syntax(key, value, width)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 105 def pre_ruby19_syntax(key, value, width) awesome_key = single_line { inspector.awesome(key) } "#{align(awesome_key, width)}#{colorize(' => ', :hash)}#{inspector.awesome(value)}" end
printable_hash()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 45 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, is_hash: true) : data end
printable_keys()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 82 def printable_keys keys = hash.keys keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys] keys.map! do |key| single_line do [key, hash[key]] end end end
ruby19_syntax(key, value, width)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 98 def ruby19_syntax(key, value, width) # Move the colon to the right side of the symbol awesome_key = inspector.awesome(key).sub(/#{key.inspect}/, "#{key}:") "#{align(awesome_key, width)} #{inspector.awesome(value)}" end
simple_hash()
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 41 def simple_hash "{ #{printable_hash.join(', ')} }" end
single_line() { || ... }
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 118 def single_line multiline = options[:multiline] options[:multiline] = false yield ensure options[:multiline] = multiline end
symbol?(key)
click to toggle source
# File lib/amazing_print/formatters/hash_formatter.rb, line 94 def symbol?(key) key.is_a?(Symbol) end