class RBON::Dump
Public Class Methods
new(tab:' ')
click to toggle source
# File lib/rbon/dump.rb, line 6 def initialize(tab:' ') @tab, @io = tab, nil end
Public Instance Methods
dump(object, io:StringIO.new)
click to toggle source
# File lib/rbon/dump.rb, line 10 def dump(object, io:StringIO.new) @io = io traverse(object,'') @io.print "\n" @io.respond_to?(:string)? @io.string : nil end
Also aliased as: pretty_generate
Private Instance Methods
array(array, indent)
click to toggle source
# File lib/rbon/dump.rb, line 42 def array(array, indent) if array.empty? @io.print '[]' else @io.print "[\n"+indent+@tab traverse(array[0], indent+@tab) array[1..-1].each do |object| @io.print ",\n"+indent+@tab traverse(object, indent+@tab) end @io.print "\n"+indent+']' end end
hash(hash, indent)
click to toggle source
# File lib/rbon/dump.rb, line 56 def hash(hash, indent) if hash.empty? @io.print '{}' else @io.print "{\n"+indent+@tab array = hash.to_a key_object(*array[0], indent+@tab) array[1..-1].each do |key,object| @io.print ",\n"+indent+@tab key_object(key,object, indent+@tab) end @io.print "\n"+indent+'}' end end
item(item, indent)
click to toggle source
# File lib/rbon/dump.rb, line 33 def item(item, indent) case item when String @io.print (item=='')? '""' : item.lines.map{_1.inspect}.join(" +\n"+indent) else @io.print item.inspect end end
key_object(key, object, indent)
click to toggle source
# File lib/rbon/dump.rb, line 71 def key_object(key, object, indent) raise RBON::Dump::Error, "Bad Key #{key.class}: #{key.inspect}" unless key.is_a?(Symbol) and key.match?('^\w+[?!]?$') @io.print "#{key}: " traverse(object, indent) end
traverse(object, indent)
click to toggle source
# File lib/rbon/dump.rb, line 20 def traverse(object, indent) case object when Hash hash(object, indent) when Array array(object, indent) when Symbol, String, Integer, Float, NilClass, TrueClass, FalseClass item(object, indent) else raise RBON::Dump::Error, "Unsupported class #{object.class}: #{object.inspect}" end end