Class: AmazingPrint::Formatters::HashFormatter

Inherits:
BaseFormatter show all
Includes:
JSONHelper
Defined in:
lib/amazing_print/formatters/hash_formatter.rb

Defined Under Namespace

Classes: InvalidHashFormatError

Constant Summary collapse

VALID_HASH_FORMATS =
i[json rocket symbol].freeze

Constants included from JSONHelper

JSONHelper::VALUE_CLASSES_NOT_TO_CONVERT

Constants inherited from BaseFormatter

BaseFormatter::DEFAULT_LIMIT_SIZE, BaseFormatter::INDENT_CACHE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from JSONHelper

#json_awesome

Methods inherited from BaseFormatter

#align, #colorless, #colorless_size, #get_limit_size, #indent, #indentation, #indented, #limited, #method_tuple, #outdent, #should_be_limited?

Methods included from Colorize

#colorize

Constructor Details

#initialize(hash, inspector) ⇒ HashFormatter

Returns a new instance of HashFormatter.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/amazing_print/formatters/hash_formatter.rb', line 17

def initialize(hash, inspector)
  super()
  @hash = hash
  @inspector = inspector
  @options = inspector.options

  return if VALID_HASH_FORMATS.include?(options[:hash_format])

  raise(InvalidHashFormatError, "Invalid hash_format: #{options[:hash_format].inspect}. " \
                                "Must be one of #{VALID_HASH_FORMATS}")
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



15
16
17
# File 'lib/amazing_print/formatters/hash_formatter.rb', line 15

def hash
  @hash
end

#inspectorObject (readonly)

Returns the value of attribute inspector.



15
16
17
# File 'lib/amazing_print/formatters/hash_formatter.rb', line 15

def inspector
  @inspector
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/amazing_print/formatters/hash_formatter.rb', line 15

def options
  @options
end

Instance Method Details

#formatObject



29
30
31
32
33
34
35
36
37
# File 'lib/amazing_print/formatters/hash_formatter.rb', line 29

def format
  if hash.empty?
    empty_hash
  elsif multiline_hash?
    multiline_hash
  else
    simple_hash
  end
end