Class: Veneer::Base::InstanceWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/veneer/base/instance_wrapper.rb

Overview

required methods

save save! # failure should raise ::Veneer::Errors::NotSaved update destroy new_record?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(instance, opts = {}) ⇒ InstanceWrapper

Returns a new instance of InstanceWrapper.



13
14
15
# File 'lib/veneer/base/instance_wrapper.rb', line 13

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



49
50
51
# File 'lib/veneer/base/instance_wrapper.rb', line 49

def method_missing(*args, &blk)
  instance.send(*args, &blk)
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



12
13
14
# File 'lib/veneer/base/instance_wrapper.rb', line 12

def instance
  @instance
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/veneer/base/instance_wrapper.rb', line 12

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object

Checks equality of the instances



23
24
25
26
27
28
29
30
# File 'lib/veneer/base/instance_wrapper.rb', line 23

def ==(other)
  case other
  when Veneer::Base::InstanceWrapper
    instance == other.instance
  else
    instance == other
  end
end

#classObject



17
18
19
# File 'lib/veneer/base/instance_wrapper.rb', line 17

def class
  @instance.class
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



37
38
39
40
41
42
# File 'lib/veneer/base/instance_wrapper.rb', line 37

def update(attributes = {})
  attributes.each do |attr,value|
    instance.send(:"#{attr}=",value)
  end
  save
end

#update!(attributes = {}) ⇒ Object



44
45
46
# File 'lib/veneer/base/instance_wrapper.rb', line 44

def update!(attributes = {})
  raise Veneer::Errors::NotSaved unless update(attributes)
end