Class: AutoBuild::HasOneHook

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name) ⇒ HasOneHook

Returns a new instance of HasOneHook.



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

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

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



3
4
5
# File 'lib/auto_build/has_one_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_one_hook.rb', line 3

def model
  @model
end

Instance Method Details

#attachObject



10
11
12
13
14
15
16
17
# File 'lib/auto_build/has_one_hook.rb', line 10

def attach
  hook_code = code
  model.class_eval do
    after_initialize do |record|
      record.instance_eval(hook_code)
    end
  end
end

#codeObject



19
20
21
# File 'lib/auto_build/has_one_hook.rb', line 19

def code
  "self.#{association_name} ||= build_#{association_name}"
end