Class: ActiveFactory::ContainerEntry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attrsObject (readonly)

Returns the value of attribute attrs.



143
144
145
# File 'lib/active_factory.rb', line 143

def attrs
  @attrs
end

#modelObject (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_saveObject



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

#buildObject



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

#saveObject



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