Class: AmazingPrint::Formatters::StructFormatter
- Inherits:
-
BaseFormatter
- Object
- BaseFormatter
- AmazingPrint::Formatters::StructFormatter
- Defined in:
- lib/amazing_print/formatters/struct_formatter.rb
Constant Summary
Constants inherited from BaseFormatter
BaseFormatter::DEFAULT_LIMIT_SIZE, BaseFormatter::INDENT_CACHE
Instance Attribute Summary collapse
-
#inspector ⇒ Object
readonly
Returns the value of attribute inspector.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#struct ⇒ Object
readonly
Returns the value of attribute struct.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #format ⇒ Object
-
#initialize(struct, inspector) ⇒ StructFormatter
constructor
A new instance of StructFormatter.
Methods inherited from BaseFormatter
#align, #get_limit_size, #indent, #indentation, #indented, #limited, #method_tuple, #outdent, #should_be_limited?
Methods included from Colorize
Constructor Details
#initialize(struct, inspector) ⇒ StructFormatter
Returns a new instance of StructFormatter.
10 11 12 13 14 15 16 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 10 def initialize(struct, inspector) super() @struct = struct @variables = struct.members @inspector = inspector @options = inspector. end |
Instance Attribute Details
#inspector ⇒ Object (readonly)
Returns the value of attribute inspector.
8 9 10 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 8 def inspector @inspector end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 8 def @options end |
#struct ⇒ Object (readonly)
Returns the value of attribute struct.
8 9 10 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 8 def struct @struct end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
8 9 10 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 8 def variables @variables end |
Instance Method Details
#format ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/amazing_print/formatters/struct_formatter.rb', line 18 def format vars = variables.map do |var| property = var.to_s[1..-1].to_sym # to_s because of some monkey patching done by Puppet. accessor = if struct.respond_to?(:"#{property}=") struct.respond_to?(property) ? :accessor : :writer else struct.respond_to?(property) ? :reader : nil end if accessor ["attr_#{accessor} :#{property}", var] else [var.to_s, var] end end data = vars.sort.map do |declaration, var| key = left_aligned do align(declaration, declaration.size) end unless [:plain] key = if key =~ /(@\w+)/ key.sub(Regexp.last_match(1), colorize(Regexp.last_match(1), :variable)) else key.sub(/(attr_\w+)\s(:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}") end end indented do key + colorize(' = ', :hash) + inspector.awesome(struct.send(var)) end end if [:multiline] "#<#{awesome_instance}\n#{data.join(%(,\n))}\n#{outdent}>" else "#<#{awesome_instance} #{data.join(', ')}>" end end |