Class: Veneer::Base::InstanceWrapper

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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



51
52
53
# File 'lib/veneer/base/instance_wrapper.rb', line 51

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

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



4
5
6
# File 'lib/veneer/base/instance_wrapper.rb', line 4

def instance
  @instance
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/veneer/base/instance_wrapper.rb', line 4

def options
  @options
end

Instance Method Details

#==(other) ⇒ Object

Checks equality of the instances



29
30
31
32
33
34
35
36
# File 'lib/veneer/base/instance_wrapper.rb', line 29

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

#classObject



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
22
23
24
25
# File 'lib/veneer/base/instance_wrapper.rb', line 13

def handle_before_save_error(e)
  if instance.respond_to?(:errors) && instance.errors.respond_to?(:add)
    case e.message
    when Array
      instance.errors.add(e.message[0], e.message[1])
    when String
      instance.errors.add("", e.message)
    end
  else
    ::STDOUT.puts e.message
  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



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

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