Class: NestedRecord::Methods

Inherits:
Module
  • Object
show all
Defined in:
lib/nested_record/methods.rb,
lib/nested_record/methods/one.rb,
lib/nested_record/methods/many.rb

Direct Known Subclasses

Many, One

Defined Under Namespace

Classes: Many, One

Instance Method Summary collapse

Constructor Details

#initialize(setup) ⇒ Methods

Returns a new instance of Methods.



2
3
4
# File 'lib/nested_record/methods.rb', line 2

def initialize(setup)
  @setup = setup
end

Instance Method Details

#attributes_writer_method_nameObject



41
42
43
# File 'lib/nested_record/methods.rb', line 41

def attributes_writer_method_name
  :"#{@setup.name}_attributes="
end

#define(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nested_record/methods.rb', line 6

def define(name)
  method_name = public_send("#{name}_method_name")
  method_body = (bodym = public_method("#{name}_method_body")).call
  case method_body
  when Proc
    define_method(method_name, &method_body)
  when String
    location = bodym.source_location
    location[1] += 1
    module_eval <<~RUBY, *location
      def #{method_name}
        #{method_body}
      end
    RUBY
  else
    fail
  end
end

#define_attributes_writer_methodObject



45
46
47
48
49
50
51
52
# File 'lib/nested_record/methods.rb', line 45

def define_attributes_writer_method
  case @setup.attributes_writer_strategy
  when :rewrite
    alias_method attributes_writer_method_name, rewrite_attributes_method_name
  when :upsert
    alias_method attributes_writer_method_name, upsert_attributes_method_name
  end
end

#reader_method_nameObject



25
26
27
# File 'lib/nested_record/methods.rb', line 25

def reader_method_name
  @setup.name
end

#rewrite_attributes_method_nameObject



37
38
39
# File 'lib/nested_record/methods.rb', line 37

def rewrite_attributes_method_name
  :"rewrite_#{@setup.name}_attributes"
end

#upsert_attributes_method_nameObject



33
34
35
# File 'lib/nested_record/methods.rb', line 33

def upsert_attributes_method_name
  :"upsert_#{@setup.name}_attributes"
end

#writer_method_nameObject



29
30
31
# File 'lib/nested_record/methods.rb', line 29

def writer_method_name
  :"#{@setup.name}="
end