-
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
Open
abhionlyone
wants to merge
5
commits into
ruby:master
Choose a base branch
from
abhionlyone:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
30327ee
Recursively converting child hash attributes to OpenStruct.
abhionlyone 252ff50
Handling hash attributes in method_missing
abhionlyone 50ed63e
Make recursiveness optional. Default is set to false.
abhionlyone 852f0e3
Removed attr_reader for recursive; Also using respond_to?(:to_hash) i…
abhionlyone 388869c
Passing options as an argument to initializer instead of is_recursive,
abhionlyone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Removed attr_reader for recursive; Also using respond_to?(:to_hash) i…
…nstead of is_a?(Hash)
- Loading branch information
commit 852f0e3fa00d9fe148047df2f2d629d042509be0
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,8 +73,6 @@ | |
| # 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 | ||
| # object will have no attributes. | ||
|
|
@@ -95,7 +93,7 @@ def initialize(hash=nil, is_recursive=false) | |
| if hash | ||
| hash.each_pair do |k, v| | ||
| k = k.to_sym | ||
| @table[k] = (recursive && v.is_a?(Hash)) ? OpenStruct.new(v,true) : v | ||
| @table[k] = (@recursive && v.respond_to?(:to_hash)) ? OpenStruct.new(v,true) : v | ||
| end | ||
| end | ||
| end | ||
|
|
@@ -204,7 +202,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)] = (recursive && args[0].is_a?(Hash)) ? OpenStruct.new(args[0],true) : args[0] | ||
| modifiable?[new_ostruct_member!(mname)] = (@recursive && args[0].respond_to?(:to_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? | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenStruct.new(v, options)orOpenStruct.new(v, recursive: true)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it should be
OpenStruct.new(v, options). Will update the PR in a bit.