Class: Superstore::Associations::Builder::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/superstore/associations/builder/association.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Association

Returns a new instance of Association.



8
9
10
# File 'lib/superstore/associations/builder/association.rb', line 8

def initialize(model, name, options)
  @model, @name, @options = model, name, options
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/superstore/associations/builder/association.rb', line 7

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/superstore/associations/builder/association.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/superstore/associations/builder/association.rb', line 7

def options
  @options
end

Class Method Details

.build(model, name, options) ⇒ Object



3
4
5
# File 'lib/superstore/associations/builder/association.rb', line 3

def self.build(model, name, options)
  new(model, name, options).build
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
# File 'lib/superstore/associations/builder/association.rb', line 12

def build
  define_writer
  define_reader

  reflection = Superstore::Associations::Reflection.new(macro, name, model, options)
  ActiveRecord::Reflection.add_reflection model, name, reflection
end

#define_readerObject



31
32
33
34
35
36
# File 'lib/superstore/associations/builder/association.rb', line 31

def define_reader
  name = self.name
  mixin.redefine_method(name) do
    association(name).reader
  end
end

#define_writerObject



24
25
26
27
28
29
# File 'lib/superstore/associations/builder/association.rb', line 24

def define_writer
  name = self.name
  mixin.redefine_method("#{name}=") do |records|
    association(name).writer(records)
  end
end

#mixinObject



20
21
22
# File 'lib/superstore/associations/builder/association.rb', line 20

def mixin
  model.generated_association_methods
end