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 9
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 15
def format
  return "{}" if hash == {}

  keys = hash.keys
  keys = keys.sort { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]
  data = keys.map do |key|
    plain_single_line do
      [ inspector.awesome(key), hash[key] ]
    end
  end

  width = data.map { |key, | key.size }.max || 0
  width += indentation if options[:indent] > 0

  data = data.map do |key, value|
    indented do
      align(key, width) << colorize(" => ", :hash) << inspector.awesome(value)
    end
  end

  data = limited(data, width, :hash => true) if should_be_limited?
  if options[:multiline]
    "{\n" << data.join(",\n") << "\n#{outdent}}"
  else
    "{ #{data.join(', ')} }"
  end
end

Private Instance Methods

plain_single_line() { || ... } click to toggle source
# File lib/awesome_print/formatters/hash_formatter.rb, line 45
def plain_single_line
  plain, multiline = options[:plain], options[:multiline]
  options[:plain], options[:multiline] = true, false
  yield
ensure
  options[:plain], options[:multiline] = plain, multiline
end