Class: RecursiveOpenStruct
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- RecursiveOpenStruct
- Defined in:
- lib/recursive_open_struct.rb
Constant Summary collapse
- VERSION =
"0.4.0"
Instance Method Summary collapse
- #debug_inspect(io = STDOUT, indent_level = 0, recursion_limit = 12) ⇒ Object
- #display_recursive_open_struct(io, ostrct_or_hash, indent_level, recursion_limit) ⇒ Object
-
#initialize(h = nil, args = {}) ⇒ RecursiveOpenStruct
constructor
A new instance of RecursiveOpenStruct.
- #new_ostruct_member(name) ⇒ Object
- #to_h ⇒ Object
Constructor Details
#initialize(h = nil, args = {}) ⇒ RecursiveOpenStruct
6 7 8 9 |
# File 'lib/recursive_open_struct.rb', line 6 def initialize(h=nil, args={}) @recurse_over_arrays = args.fetch(:recurse_over_arrays,false) super(h) end |
Instance Method Details
#debug_inspect(io = STDOUT, indent_level = 0, recursion_limit = 12) ⇒ Object
36 37 38 |
# File 'lib/recursive_open_struct.rb', line 36 def debug_inspect(io = STDOUT, indent_level = 0, recursion_limit = 12) display_recursive_open_struct(io, @table, indent_level, recursion_limit) end |
#display_recursive_open_struct(io, ostrct_or_hash, indent_level, recursion_limit) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/recursive_open_struct.rb', line 40 def display_recursive_open_struct(io, ostrct_or_hash, indent_level, recursion_limit) if recursion_limit <= 0 then # protection against recursive structure (like in the tests) io.puts ' '*indent_level + '(recursion limit reached)' else #puts ostrct_or_hash.inspect if ostrct_or_hash.is_a?(RecursiveOpenStruct) then ostrct_or_hash = ostrct_or_hash.marshal_dump end # We'll display the key values like this : key = value # to align display, we look for the maximum key length of the data that will be displayed # (everything except hashes) data_indent = ostrct_or_hash \ .reject { |k, v| v.is_a?(RecursiveOpenStruct) || v.is_a?(Hash) } \ .max {|a,b| a[0].to_s.length <=> b[0].to_s.length}[0].to_s.length # puts "max length = #{data_indent}" ostrct_or_hash.each do |key, value| if (value.is_a?(RecursiveOpenStruct) || value.is_a?(Hash)) then io.puts ' '*indent_level + key.to_s + '.' display_recursive_open_struct(io, value, indent_level + 1, recursion_limit - 1) else io.puts ' '*indent_level + key.to_s + ' '*(data_indent - key.to_s.length) + ' = ' + value.inspect end end end true end |
#new_ostruct_member(name) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/recursive_open_struct.rb', line 15 def new_ostruct_member(name) name = name.to_sym unless self.respond_to?(name) class << self; self; end.class_eval do define_method(name) do v = @table[name] if v.is_a?(Hash) RecursiveOpenStruct.new(v) elsif v.is_a?(Array) and @recurse_over_arrays v.map { |a| (a.is_a? Hash) ? RecursiveOpenStruct.new(a, :recurse_over_arrays => true) : a } else v end end define_method("#{name}=") { |x| modifiable[name] = x } define_method("#{name}_as_a_hash") { @table[name] } end end name end |
#to_h ⇒ Object
11 12 13 |
# File 'lib/recursive_open_struct.rb', line 11 def to_h @table.dup end |