Class: ActiveFactory::ContainerEntry
- Inherits:
-
Object
- Object
- ActiveFactory::ContainerEntry
- Defined in:
- lib/active_factory.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #before_save ⇒ Object
- #build ⇒ Object
-
#initialize(index, factory, context) ⇒ ContainerEntry
constructor
A new instance of ContainerEntry.
- #merge(hash) ⇒ Object
- #save ⇒ Object
Constructor Details
#initialize(index, factory, context) ⇒ ContainerEntry
Returns a new instance of ContainerEntry.
145 146 147 148 149 150 |
# File 'lib/active_factory.rb', line 145 def initialize index, factory, context @index = index @factory = factory @attrs = HashStruct[factory.attributes_for(index)] @context = context end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
143 144 145 |
# File 'lib/active_factory.rb', line 143 def attrs @attrs end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
143 144 145 |
# File 'lib/active_factory.rb', line 143 def model @model end |
Instance Method Details
#before_save ⇒ Object
165 166 167 168 169 |
# File 'lib/active_factory.rb', line 165 def before_save if @model and not @saved @factory.apply_before_save @index, @context, @model end end |
#build ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/active_factory.rb', line 156 def build unless @model @model = @factory.model_class.new @attrs.each_pair { |k,v| @model.send "#{k}=", v } @factory.apply_after_build @index, @context, @model end end |
#merge(hash) ⇒ Object
152 153 154 |
# File 'lib/active_factory.rb', line 152 def merge hash @attrs = @attrs.merge hash end |
#save ⇒ Object
171 172 173 174 175 176 |
# File 'lib/active_factory.rb', line 171 def save if @model and not @saved @model.save! @saved = true end end |