Uh oh!
There was an error while loading. Please reload this page.
Recursively converting child hash attributes to OpenStruct. - #5
Recursively converting child hash attributes to OpenStruct. #5abhionlyone wants to merge 5 commits into
Conversation
abhionlyone
commented
Oct 15, 2018
Hi @hsbt , Did you get a chance to review this PR? What do you think about the changes? Regards, |
esparta
commented
Oct 15, 2018
No maintainer or someone to have decision, but before going forward I'd suggest two things:
Additionally, as being a new feature this should be proposed via https://bugs.ruby-lang.org ticket, will be discussed there (if not rejected before). |
marcandre
commented
Oct 15, 2018
It's fine to propose a feature/change without having tests, etc., as a way to first check if the feature is acceptable or not. In this case I believe it isn't, for compatibility reasons. In the example above, |
abhionlyone
commented
Oct 15, 2018
@marcandre Thanks for the response!! I completely agree with your feedback. But there are a lot of people out there expecting OpenStruct to recursively convert a child hash to another OpenStruct object. So, why not provide them with an option to recursively convert child hash objects to OpenStruct object? Maybe allowing users to pass additional options to constructor would help? Like this: |
marcandre
commented
Oct 15, 2018
Adding a The official forum to get a new feature accepted is https://bugs.ruby-lang.org/ . Best thing at this point would be to make the feature request there to see if there are any objections. If you'd like to work on the implementation, you can amend this PR too. You'll need tests, also I'd use |
abhionlyone
commented
Oct 15, 2018
@marcandre Thanks! Like you suggested I will create a feature request on https://bugs.ruby-lang.org/ 😃 |
| hash.each_pair do |k, v| | ||
| k = k.to_sym | ||
| @table[k] = v | ||
| @table[k] = (recursive && v.is_a?(Hash)) ? OpenStruct.new(v, recursive: true) : v |
There was a problem hiding this comment.
recursive is not defined as a keyword argument.
Uh oh!
There was an error while loading. Please reload this page.
| 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 |
There was a problem hiding this comment.
As I pointed out, best use respond_to?(:to_hash) instead of is_a?(Hash)
There was a problem hiding this comment.
Sorry missed that. Updated the code!
…nstead of is_a?(Hash)
imnithin
commented
Oct 16, 2018
+1 for the PR |
| # data # => #<OpenStruct country="Australia", capital="Canberra"> | ||
| # | ||
| def initialize(hash=nil) | ||
| def initialize(hash=nil, is_recursive=false) |
There was a problem hiding this comment.
Would be more readable to use a keyword argument I'd say
There was a problem hiding this comment.
if we use keyword argument the below test will fail:
def test_accessor_defines_method
os = OpenStruct.new(foo: 42)
assert os.respond_to? :foo
assert_equal([], os.singleton_methods)
assert_equal(42, os.foo)
assert_equal([:foo, :foo=], os.singleton_methods.sort)
end
But for better readability I changed the constructor to this:
def initialize(hash=nil, options={recursive: false})
@table = {}
@recursive = options.fetch(:recursive, false)
if hash
hash.each_pair do |k, v|
k = k.to_sym
@table[k] = (@recursive && v.respond_to?(:to_hash)) ? OpenStruct.new(v,true) : v
end
end
end
bd2c661 to
696ba34Compareabhionlyone
commented
Oct 16, 2018
Just FYI, A feature request has been created here https://bugs.ruby-lang.org/issues/15225 |
| hash.each_pair do |k, v| | ||
| k = k.to_sym | ||
| @table[k] = v | ||
| @table[k] = (@recursive && v.respond_to?(:to_hash)) ? OpenStruct.new(v,true) : v |
There was a problem hiding this comment.
OpenStruct.new(v, options) or OpenStruct.new(v, recursive: true)
There was a problem hiding this comment.
Yes, it should be OpenStruct.new(v, options). Will update the PR in a bit.
| 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].respond_to?(:to_hash)) ? OpenStruct.new(args[0],true) : args[0] |
There was a problem hiding this comment.
Same hereOpenStruct.new(v, recursive: true)
n-rodriguez
commented
Jan 2, 2021
Hi there! Any news? |
marcandre
commented
Jan 3, 2021
I updated https://bugs.ruby-lang.org/issues/15225 |
This PR can enable something like this: