Module: Sumaki::Model::Associations::AccessorAdder::Singular

Defined in:
lib/sumaki/model/associations.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.add(methods_module, reflections, reflection) ⇒ Object



17
18
19
20
21
22
# File 'lib/sumaki/model/associations.rb', line 17

def add(methods_module, reflections, reflection)
  add_getter(methods_module, reflection.name)
  add_builder(methods_module, reflection.name)

  reflections[reflection.name] = reflection
end

.add_builder(methods_module, name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/sumaki/model/associations.rb', line 34

def add_builder(methods_module, name)
  methods_module.module_eval <<~RUBY, __FILE__, __LINE__ + 1
    def build_#{name}(attrs = {})              # def build_author(attrs = {})
      association(:#{name}).build_model(attrs) #   association(:author).build_model(attrs)
    end                                        # end
  RUBY
end

.add_getter(methods_module, name) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/sumaki/model/associations.rb', line 26

def add_getter(methods_module, name)
  methods_module.module_eval <<~RUBY, __FILE__, __LINE__ + 1
    def #{name}                   # def author
      association(:#{name}).model #   association(:author).model
    end                           # end
  RUBY
end