Class: AutoBuild::HasManyHook

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_build/has_many_hook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ HasManyHook

Returns a new instance of HasManyHook.



5
6
7
8
9
# File 'lib/auto_build/has_many_hook.rb', line 5

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

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



3
4
5
# File 'lib/auto_build/has_many_hook.rb', line 3

def association_name
  @association_name
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/auto_build/has_many_hook.rb', line 3

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/auto_build/has_many_hook.rb', line 3

def options
  @options
end

Instance Method Details

#attachObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/auto_build/has_many_hook.rb', line 11

def attach
  hook_code = code
  name = association_name
  record_options = options

  model.class_eval do
    after_initialize do |record|
      count = number_of_records_to_create(name, record_options)
      record.instance_eval(hook_code * count)
    end
  end
end

#codeObject



24
25
26
# File 'lib/auto_build/has_many_hook.rb', line 24

def code
  "self.#{association_name}.build;"
end