Class: Menilite::Model::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/menilite/model/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Association



8
9
10
11
# File 'lib/menilite/model/association.rb', line 8

def initialize(model_class)
  @model_class = model_class
  @model = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args, &block) ⇒ Object (private)



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/menilite/model/association.rb', line 32

def method_missing(method_sym, *args, &block)
  field_name = method_sym.to_s
  field_name = field_name[0, field_name.size - 1] if field_name.chars.last == '='
  field = @model_class.field_info[field_name]
  if field
    raise UnacquiredDataAccess.new unless @model
    @model.send(method_sym, *args)
  elsif @model && @model.respond_to?(method_sym)
    @model.send(method_sym, *args)
  else
    super
  end
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



6
7
8
# File 'lib/menilite/model/association.rb', line 6

def model_class
  @model_class
end

Instance Method Details

#assign(model) ⇒ Object



13
14
15
# File 'lib/menilite/model/association.rb', line 13

def assign(model)
  @model = model
end

#idObject



21
22
23
# File 'lib/menilite/model/association.rb', line 21

def id
  @model && @model.id
end

#load(id) ⇒ Object



17
18
19
# File 'lib/menilite/model/association.rb', line 17

def load(id)
  assign @model_class.find(id)
end

#to_hObject



25
26
27
28
# File 'lib/menilite/model/association.rb', line 25

def to_h
  raise UnacquiredDataAccess.new unless @model
  @model.to_h
end