-
Notifications
You must be signed in to change notification settings - Fork 32
Recursively converting child hash attributes to OpenStruct. #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
30327ee
252ff50
50ed63e
852f0e3
388869c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,6 +73,7 @@ | |
| # of these properties compared to using a Hash or a Struct. | ||
| # | ||
| class OpenStruct | ||
| attr_reader :recursive | ||
|
|
||
| # | ||
| # Creates a new OpenStruct object. By default, the resulting OpenStruct | ||
|
|
@@ -88,12 +89,13 @@ class OpenStruct | |
| # | ||
| # data # => #<OpenStruct country="Australia", capital="Canberra"> | ||
| # | ||
| def initialize(hash=nil) | ||
| def initialize(hash=nil, is_recursive=false) | ||
|
||
| @table = {} | ||
| @recursive = is_recursive | ||
| if hash | ||
| hash.each_pair do |k, v| | ||
| k = k.to_sym | ||
| @table[k] = v | ||
| @table[k] = (recursive && v.is_a?(Hash)) ? OpenStruct.new(v,true) : v | ||
|
||
| end | ||
| end | ||
| end | ||
|
|
@@ -202,7 +204,7 @@ def method_missing(mid, *args) # :nodoc: | |
| if len != 1 | ||
| raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) | ||
| end | ||
| modifiable?[new_ostruct_member!(mname)] = args[0] | ||
| modifiable?[new_ostruct_member!(mname)] = (recursive && args[0].is_a?(Hash)) ? OpenStruct.new(args[0],true) : args[0] | ||
| elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid # | ||
| if @table.key?(mid) | ||
| new_ostruct_member!(mid) unless frozen? | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.