diff options
| author | Vladimir Kochnev <hashtable@yandex.ru> | 2015-11-15 16:57:51 +0300 |
|---|---|---|
| committer | Vladimir Kochnev <hashtable@yandex.ru> | 2019-01-28 21:14:08 +0300 |
| commit | 250f174f48a7115c832690fd052dcaf63b6debc9 (patch) | |
| tree | 84c55562cdd1281072bcd84114d11859a7f5e528 /lib/hashie.rb | |
| parent | cd30488f9e4f4cbdfe0ccada40297e6f6d7e5610 (diff) | |
| download | hashie-250f174f48a7115c832690fd052dcaf63b6debc9.tar.gz | |
Add Hashie::Extensions::Mash::DefineAccessors.
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` —
https://github.com/ruby/ruby/pull/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.
Diffstat (limited to 'lib/hashie.rb')
| -rw-r--r-- | lib/hashie.rb | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/lib/hashie.rb b/lib/hashie.rb index b6e1d13..fd93e05 100644 --- a/lib/hashie.rb +++ b/lib/hashie.rb @@ -47,6 +47,7 @@ module Hashie autoload :KeepOriginalKeys, 'hashie/extensions/mash/keep_original_keys' autoload :SafeAssignment, 'hashie/extensions/mash/safe_assignment' autoload :SymbolizeKeys, 'hashie/extensions/mash/symbolize_keys' + autoload :DefineAccessors, 'hashie/extensions/mash/define_accessors' end module Array |
