Class: AmazingPrint::Formatter

Inherits:
Object
  • Object
show all
Includes:
ActiveRecord, ActiveSupport, Colorize, MongoMapper, Mongoid, NoBrainer, Nokogiri, OpenStruct, Ripple, Sequel
Defined in:
lib/amazing_print/formatter.rb

Constant Summary collapse

CORE_FORMATTERS =
%i[array bigdecimal class dir file hash method rational set struct unboundmethod].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport

#awesome_active_support_time, #awesome_hash_with_indifferent_access, #cast_with_active_support, included

Methods included from ActiveRecord

#cast_with_active_record, included

Methods included from MongoMapper

#awesome_mongo_mapper_association, #awesome_mongo_mapper_bson_id, #awesome_mongo_mapper_class, #awesome_mongo_mapper_instance, #cast_with_mongo_mapper, included

Methods included from NoBrainer

#awesome_nobrainer_class, #awesome_nobrainer_document, #cast_with_nobrainer, included

Methods included from Nokogiri

#awesome_nokogiri_xml_node, #cast_with_nokogiri, included

Methods included from OpenStruct

#awesome_open_struct_instance, #cast_with_ostruct, included

Methods included from Mongoid

#awesome_mongoid_bson_id, #awesome_mongoid_class, #awesome_mongoid_document, #cast_with_mongoid, included

Methods included from Sequel

#awesome_sequel_dataset, #awesome_sequel_document, #awesome_sequel_model_class, #cast_with_sequel, included

Methods included from Ripple

#cast_with_ripple, included

Methods included from Colorize

#colorize

Constructor Details

#initialize(inspector) ⇒ Formatter

Returns a new instance of Formatter.



18
19
20
21
# File 'lib/amazing_print/formatter.rb', line 18

def initialize(inspector)
  @inspector   = inspector
  @options     = inspector.options
end

Instance Attribute Details

#inspectorObject (readonly)

Returns the value of attribute inspector.



14
15
16
# File 'lib/amazing_print/formatter.rb', line 14

def inspector
  @inspector
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/amazing_print/formatter.rb', line 14

def options
  @options
end

Instance Method Details

#cast(_object, type) ⇒ Object

Hook this when adding custom formatters. Check out lib/amazing_print/ext directory for custom formatters that ship with amazing_print.




37
38
39
# File 'lib/amazing_print/formatter.rb', line 37

def cast(_object, type)
  CORE_FORMATTERS.include?(type) ? type : :self
end

#format(object, type = nil) ⇒ Object

Main entry point to format an object.




25
26
27
28
29
30
31
32
# File 'lib/amazing_print/formatter.rb', line 25

def format(object, type = nil)
  core_class = cast(object, type)
  if core_class == :self
    awesome_self(object, type) # Catch all that falls back to object.inspect.
  else
    send(:"awesome_#{core_class}", object) # Core formatters.
  end
end