Skip to content
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

MethodAccess should inject itself into subhashes #347

Open
dblock opened this issue Feb 1, 2016 · 2 comments
Open

MethodAccess should inject itself into subhashes #347

dblock opened this issue Feb 1, 2016 · 2 comments

Comments

@dblock
Copy link
Member

dblock commented Feb 1, 2016

Just like Hashie::Extensions::IndifferentAccess.

class MyHash < Hash
  include Hashie::Extensions::MergeInitializer
  include Hashie::Extensions::IndifferentAccess
  include Hashie::Extensions::MethodAccess

end

h = MyHash.new(foo: { bar: 1 })

2.0.0-p598 :010 > h[:foo]
 => {"bar"=>1} 
2.0.0-p598 :011 > h['foo']
 => {"bar"=>1} 
2.0.0-p598 :009 > h.foo
 => {"bar"=>1} 

2.0.0-p598 :013 > h[:foo][:bar]
 => 1 
2.0.0-p598 :014 > h['foo']['bar']
 => 1 
2.0.0-p598 :015 > h.foo.bar
NoMethodError: undefined method `bar' for {"bar"=>1}:Hash
    from (irb):15
    from /Users/dblock/.rvm/rubies/ruby-2.0.0-p598/bin/irb:12:in `<main>'

vs. Mash

2.0.0-p598 :017 > h = Hashie::Mash.new(foo: { bar: 1 })
 => #<Hashie::Mash foo=#<Hashie::Mash bar=1>> 
@marshall-lee
Copy link
Contributor

I don't think it should.

Comparing to Mash your custom class lacks mash-like deep initializer which is a different concern from MethodAccess. So you just need a smart initializer extracted from Mash (just like MergeInitializer but deep), probably implemented as a separate extension.
So It's a initializer issue, not an issue of MethodAccess.

@dblock
Copy link
Member Author

dblock commented Feb 7, 2016

Fair enough @marshall-lee!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants