Module: DR::Converter
Instance Method Summary collapse
-
#to_hash(obj = nil, methods: [], recursive: false, check: false, compact: false) ⇒ Object
convert an obj to hash, using 'methods' for the methods attributes.
Instance Method Details
#to_hash(obj = nil, methods: [], recursive: false, check: false, compact: false) ⇒ Object
convert an obj to hash, using 'methods' for the methods attributes
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dr/base/converter.rb', line 5 def to_hash(obj=nil, methods:[], recursive: false, check: false, compact: false) return {} if obj.nil? obj||=self stack=[*obj] processed=[] klass=stack.first.class h={} while !stack.empty? obj=stack.shift next if processed.include?(obj) processed << obj attributes={} methods.each do |m| next if check and !obj.respond_to? m v=obj.public_send(m) attributes[m]=v if recursive vals=v.kind_of?(Enumerable) ? v.to_a.flatten : [v] vals.select! {|v| v.kind_of?(klass)} stack.concat(vals) end end attributes=attributes.values.first if compact and attributes.keys.length == 1 h[obj]=attributes end h end |