Class: Veneer::Base::InstanceWrapper
- Inherits:
-
Object
- Object
- Veneer::Base::InstanceWrapper
- Defined in:
- lib/veneer/base/instance_wrapper.rb
Direct Known Subclasses
ActiveRecord::Base::VeneerInterface::InstanceWrapper, DataMapper::Resource::VeneerInterface::InstanceWrapper, MongoMapper::Document::VeneerInterface::InstanceWrapper
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Checks equality of the instances.
- #class ⇒ Object
- #handle_before_save_error(e) ⇒ Object
-
#initialize(instance, opts = {}) ⇒ InstanceWrapper
constructor
A new instance of InstanceWrapper.
-
#method_missing(*args, &blk) ⇒ Object
send all methods to the instance.
-
#update(attributes = {}) ⇒ Object
Updates the attributes by trying to use the keys of the attributes hash as a setter method (:“#key=”) to the value of the hash then the object is saved Adapter implementors may want to overwrite may want to overwrite.
Constructor Details
#initialize(instance, opts = {}) ⇒ InstanceWrapper
Returns a new instance of InstanceWrapper.
5 6 7 |
# File 'lib/veneer/base/instance_wrapper.rb', line 5 def initialize(instance, opts = {}) @instance, @options = instance, opts end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args, &blk) ⇒ Object
send all methods to the instance
47 48 49 |
# File 'lib/veneer/base/instance_wrapper.rb', line 47 def method_missing(*args, &blk) instance.send(*args, &blk) end |
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
4 5 6 |
# File 'lib/veneer/base/instance_wrapper.rb', line 4 def instance @instance end |
#options ⇒ Object
Returns the value of attribute options.
4 5 6 |
# File 'lib/veneer/base/instance_wrapper.rb', line 4 def @options end |
Instance Method Details
#==(other) ⇒ Object
Checks equality of the instances
25 26 27 28 29 30 31 32 |
# File 'lib/veneer/base/instance_wrapper.rb', line 25 def ==(other) case other when Veneer::Base::InstanceWrapper instance == other.instance else instance == other end end |
#class ⇒ Object
9 10 11 |
# File 'lib/veneer/base/instance_wrapper.rb', line 9 def class @instance.class end |
#handle_before_save_error(e) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/veneer/base/instance_wrapper.rb', line 13 def handle_before_save_error(e) case e. when Array instance.errors.add(e.[0], e.[1]) when String instance.errors.add("", e.) end false end |
#update(attributes = {}) ⇒ Object
Updates the attributes by trying to use the keys of the attributes hash as a setter method (:“#key=”) to the value of the hash then the object is saved Adapter implementors may want to overwrite may want to overwrite
39 40 41 42 43 44 |
# File 'lib/veneer/base/instance_wrapper.rb', line 39 def update(attributes = {}) attributes.each do |attr,value| instance.send(:"#{attr}=",value) end save end |