mirror of
https://github.com/awesome-print/awesome_print
synced 2023-03-27 23:22:34 -04:00
Show attribute accessors when diplaying an arbitrary object
This commit is contained in:
parent
62041f2aa4
commit
4f163f2468
2 changed files with 49 additions and 17 deletions
|
@ -86,13 +86,8 @@ module AwesomePrint
|
||||||
width += @indentation if @options[:indent] > 0
|
width += @indentation if @options[:indent] > 0
|
||||||
|
|
||||||
data = data.map do |key, value|
|
data = data.map do |key, value|
|
||||||
if @options[:multiline]
|
|
||||||
formatted_key = (@options[:indent] >= 0 ? key.rjust(width) : indent + key.ljust(width))
|
|
||||||
else
|
|
||||||
formatted_key = key
|
|
||||||
end
|
|
||||||
indented do
|
indented do
|
||||||
formatted_key << colorize(" => ", :hash) << @inspector.awesome(value)
|
align(key, width) << colorize(" => ", :hash) << @inspector.awesome(value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if @options[:multiline]
|
if @options[:multiline]
|
||||||
|
@ -105,24 +100,40 @@ module AwesomePrint
|
||||||
# Format an object.
|
# Format an object.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def awesome_object(o)
|
def awesome_object(o)
|
||||||
vars = o.instance_variables.sort
|
vars = o.instance_variables.map do |var|
|
||||||
width = vars.map { |var| var.size }.max || 0
|
property = var[1..-1].to_sym
|
||||||
|
accessor = if o.respond_to?(:"#{property}=")
|
||||||
|
o.respond_to?(property) ? :accessor : :writer
|
||||||
|
else
|
||||||
|
o.respond_to?(property) ? :reader : nil
|
||||||
|
end
|
||||||
|
if accessor
|
||||||
|
[ "attr_#{accessor} :#{property}", var ]
|
||||||
|
else
|
||||||
|
[ var.to_s, var ]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
width = vars.map { |declaration,| declaration.size }.max || 0
|
||||||
width += @indentation if @options[:indent] > 0
|
width += @indentation if @options[:indent] > 0
|
||||||
|
|
||||||
data = vars.map do |var|
|
data = vars.sort.map do |declaration, var|
|
||||||
if @options[:multiline]
|
key = align(declaration, width)
|
||||||
formatted_key = (@options[:indent] >= 0 ? var.rjust(width) : indent + var.ljust(width))
|
unless @options[:plain]
|
||||||
else
|
if key =~ /(@\w+)/
|
||||||
formatted_key = var
|
key.sub!($1, colorize($1, :variable))
|
||||||
|
else
|
||||||
|
key.sub!(/(attr_\w+)\s(\:\w+)/, "#{colorize('\\1', :keyword)} #{colorize('\\2', :method)}")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
indented do
|
indented do
|
||||||
formatted_key << colorize(" => ", :hash) + @inspector.awesome(o.instance_variable_get(var))
|
key << colorize(" = ", :hash) + @inspector.awesome(o.instance_variable_get(var))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if @options[:multiline]
|
if @options[:multiline]
|
||||||
"{\n" << data.join(",\n") << "\n#{outdent}}"
|
"#<#{awesome_instance(o)}\n#{data.join(%Q/,\n/)}\n#{outdent}>"
|
||||||
else
|
else
|
||||||
"{ #{data.join(', ')} }"
|
"#<#{awesome_instance(o)} #{data.join(', ')}>"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -173,6 +184,12 @@ module AwesomePrint
|
||||||
end
|
end
|
||||||
alias :awesome_unboundmethod :awesome_method
|
alias :awesome_unboundmethod :awesome_method
|
||||||
|
|
||||||
|
# Format object instance.
|
||||||
|
#------------------------------------------------------------------------------
|
||||||
|
def awesome_instance(o)
|
||||||
|
"#{o.class}:0x%08x" % (o.__id__ * 2)
|
||||||
|
end
|
||||||
|
|
||||||
# Format object.methods array.
|
# Format object.methods array.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
def methods_array(a)
|
def methods_array(a)
|
||||||
|
@ -238,7 +255,20 @@ module AwesomePrint
|
||||||
@options[:plain], @options[:multiline] = plain, multiline
|
@options[:plain], @options[:multiline] = plain, multiline
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Utility methods.
|
||||||
#------------------------------------------------------------------------------
|
#------------------------------------------------------------------------------
|
||||||
|
def align(value, width)
|
||||||
|
if @options[:multiline]
|
||||||
|
if @options[:indent] >= 0
|
||||||
|
value.rjust(width)
|
||||||
|
else
|
||||||
|
indent + value.ljust(width)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
value
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def indented
|
def indented
|
||||||
@indentation += @options[:indent].abs
|
@indentation += @options[:indent].abs
|
||||||
yield
|
yield
|
||||||
|
|
|
@ -32,7 +32,9 @@ module AwesomePrint
|
||||||
:time => :greenish,
|
:time => :greenish,
|
||||||
:trueclass => :green,
|
:trueclass => :green,
|
||||||
:method => :purpleish,
|
:method => :purpleish,
|
||||||
:args => :pale
|
:args => :pale,
|
||||||
|
:keyword => :cyan,
|
||||||
|
:variable => :cyanish
|
||||||
},
|
},
|
||||||
:indent => 4,
|
:indent => 4,
|
||||||
:index => true,
|
:index => true,
|
||||||
|
|
Loading…
Reference in a new issue