Class: Dooly::IdProxy::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/dooly/id_proxy/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Base

Returns a new instance of Base.



21
22
23
24
# File 'lib/dooly/id_proxy/base.rb', line 21

def initialize(id)
  raise 'idea must have id' if id.blank?
  @id = id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



47
48
49
# File 'lib/dooly/id_proxy/base.rb', line 47

def method_missing(name, *args, &block)
  self.record.send(name, *args, &block)
end

Class Attribute Details

.findersObject Also known as: finder

Returns the value of attribute finders.



6
7
8
# File 'lib/dooly/id_proxy/base.rb', line 6

def finders
  @finders
end

.modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/dooly/id_proxy/base.rb', line 5

def model
  @model
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



19
20
21
# File 'lib/dooly/id_proxy/base.rb', line 19

def id
  @id
end

Instance Method Details

#recordObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dooly/id_proxy/base.rb', line 26

def record
  return @record if @record

  self.class.finders.each do |finder|
    f = finder.to_sym
    if self.class.model.respond_to?(f)
      @record = self.class.model.send(f, self.id)
    end
  end

  unless @record
    @record = self.class.model.find(self.id)
  end

  @record
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/dooly/id_proxy/base.rb', line 43

def respond_to?(name)
  self.record.respond_to?(name) || super
end