File tree 1 file changed +5
-3
lines changed
1 file changed +5
-3
lines changed Original file line number Diff line number Diff line change 73
73
# of these properties compared to using a Hash or a Struct.
74
74
#
75
75
class OpenStruct
76
+ attr_reader :recursive
76
77
77
78
#
78
79
# Creates a new OpenStruct object. By default, the resulting OpenStruct
@@ -88,12 +89,13 @@ class OpenStruct
88
89
#
89
90
# data # => #<OpenStruct country="Australia", capital="Canberra">
90
91
#
91
- def initialize ( hash = nil )
92
+ def initialize ( hash = nil , recursive = false )
92
93
@table = { }
94
+ @recursive = recursive
93
95
if hash
94
96
hash . each_pair do |k , v |
95
97
k = k . to_sym
96
- @table [ k ] = v . is_a? ( Hash ) ? OpenStruct . new ( v ) : v
98
+ @table [ k ] = ( recursive && v . is_a? ( Hash ) ) ? OpenStruct . new ( v , recursive : true ) : v
97
99
end
98
100
end
99
101
end
@@ -202,7 +204,7 @@ def method_missing(mid, *args) # :nodoc:
202
204
if len != 1
203
205
raise ArgumentError , "wrong number of arguments (#{ len } for 1)" , caller ( 1 )
204
206
end
205
- modifiable? [ new_ostruct_member! ( mname ) ] = args [ 0 ] . is_a? ( Hash ) ? OpenStruct . new ( args [ 0 ] ) : args [ 0 ]
207
+ modifiable? [ new_ostruct_member! ( mname ) ] = ( recursive && args [ 0 ] . is_a? ( Hash ) ) ? OpenStruct . new ( args [ 0 ] , recursive : true ) : args [ 0 ]
206
208
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
207
209
if @table . key? ( mid )
208
210
new_ostruct_member! ( mid ) unless frozen?
You can’t perform that action at this time.
0 commit comments