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

Add Hashie::Extensions::Mash::DefineAccessors. #323

Merged
merged 1 commit into from
Jan 28, 2019

Conversation

marshall-lee
Copy link
Contributor

This patch adds an extension for Mash that makes it behave like OpenStruct. It reduces overhead of method_missing? magic which is a good thing! It's inspired by the recent @sferik's work on OpenStructruby/ruby#1033.

When using it in Mash subclasses it makes them remember methods so then it's more like ActiveModel than OpenStruct in this case.

To use it like OpenStruct one could use this shortcut:

{ foo: 1, bar: 2 }.to_mash.with_accessors!

Implementation details:

It injects to class an anonymous module that stores accessor method definitions. This is inspired by ActiveModel / ActiveRecord. It allows to override accessors in subclass and call them via super if this is intended.

@dblock
Copy link
Member

dblock commented Nov 15, 2015

I like it. Needs README/CHANGELOG. Thanks!

@TylerRick
Copy link

I want this! Can I haz?

@dblock
Copy link
Member

dblock commented Jan 16, 2019

Someone finish this PR please!

@marshall-lee
Copy link
Contributor Author

Ok, let's do it :)

@marshall-lee marshall-lee force-pushed the define_accessors branch 2 times, most recently from 413468b to 8b8e542 Compare January 28, 2019 17:44
This patch adds an extension for Mash that makes it behave like
`OpenStruct`. It reduces overhead of `method_missing?` magic which is a
good thing! It's inspired by the recent @sferik's work on `OpenStruct` —
ruby/ruby#1033.

When using it in `Mash` subclasses it makes them *remember* methods so
then it's more like `ActiveModel` than `OpenStruct` in this case.

To use it like `OpenStruct` one could use this shortcut:

```ruby
{ foo: 1, bar: 2 }.to_mash.with_accessors!
```

Implementation details:

It injects to class an anonymous module that stores accessor method
definitions. This is inspired by `ActiveModel` / `ActiveRecord`. It
allows to override accessors in subclass and call them via `super` if
this is intended.
@marshall-lee
Copy link
Contributor Author

@dblock it's done!

@dblock dblock merged commit dc64b10 into hashie:master Jan 28, 2019
@dblock
Copy link
Member

dblock commented Jan 28, 2019

I merged it. Hope @michaelherold is cool with it ;)

@michaelherold
Copy link
Member

If you're good with it, I'm good with it, my friend. 😉

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

Successfully merging this pull request may close these issues.

4 participants