Class: AutoBuild::HasOneHook
- Inherits:
-
Object
- Object
- AutoBuild::HasOneHook
- Defined in:
- lib/auto_build/has_one_hook.rb
Instance Attribute Summary collapse
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #attach ⇒ Object
- #code ⇒ Object
-
#initialize(model, name) ⇒ HasOneHook
constructor
A new instance of HasOneHook.
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_name ⇒ Object (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 |
#model ⇒ Object (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
#attach ⇒ Object
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 |
#code ⇒ Object
19 20 21 |
# File 'lib/auto_build/has_one_hook.rb', line 19 def code "self.#{association_name} ||= build_#{association_name}" end |