Class: Hashie::LazyTrash
- Inherits:
-
Trash
- Object
- Trash
- Hashie::LazyTrash
- Defined in:
- lib/hashie-lazy_trash.rb
Overview
A Hashie::Trash with 2 main differences:
1. When trying to set a value that is not defined as a property, the LazyTrash just sllently ignores this value
(as opposed to Hashie::Trash, which raises an Exception)
2. You can define properties that are to be fetched lazily (on first access), i.e.
property :user_id
property :user, lazy: ->{ User.find(user_id) }
As you can see in this example, the Proc is executed in the context of the Hashie::LazyTrash instance.
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.property(property_name, options = {}) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/hashie-lazy_trash.rb', line 17 def self.property(property_name, = {}) super if [:lazy] lazy_properties[property_name.to_sym] = [:lazy] end end |
Instance Method Details
#[](property) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/hashie-lazy_trash.rb', line 25 def [](property) if !super and self.class.lazy_properties.include?(property.to_sym) self[property.to_sym] = instance_exec(&self.class.lazy_properties[property.to_sym]) else super end end |