Class: ActiveFactory::Container

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

Overview

keeps collection of created instances of the given model class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, factory, context) ⇒ Container

Returns a new instance of Container.



184
185
186
187
188
189
# File 'lib/active_factory.rb', line 184

def initialize name, factory, context
  @name = name
  @factory = factory
  @context = context
  @entries = [].freeze
end

Instance Attribute Details

#entriesObject

Returns the value of attribute entries.



181
182
183
# File 'lib/active_factory.rb', line 181

def entries
  @entries
end

#factoryObject (readonly)

Returns the value of attribute factory.



182
183
184
# File 'lib/active_factory.rb', line 182

def factory
  @factory
end

#nameObject (readonly)

Returns the value of attribute name.



182
183
184
# File 'lib/active_factory.rb', line 182

def name
  @name
end

Instance Method Details

#attrsObject



231
232
233
# File 'lib/active_factory.rb', line 231

def attrs
  @entries.map &:attrs
end

#before_saveObject



223
224
225
# File 'lib/active_factory.rb', line 223

def before_save
  @entries.each &:before_save
end

#buildObject



214
215
216
217
# File 'lib/active_factory.rb', line 214

def build
  @entries.each &:build
  self
end

#create(count) ⇒ Object



191
192
193
# File 'lib/active_factory.rb', line 191

def create count
  dup_with add_entries count
end

#make_linkerObject



219
220
221
# File 'lib/active_factory.rb', line 219

def make_linker
  Linker.new self
end

#objectsObject



235
236
237
# File 'lib/active_factory.rb', line 235

def objects
  @entries.map &:model
end

#saveObject



227
228
229
# File 'lib/active_factory.rb', line 227

def save
  @entries.each &:save
end

#singletonObject



195
196
197
198
199
200
201
202
203
# File 'lib/active_factory.rb', line 195

def singleton
  if @entries.none?
    add_entries 1
  elsif @entries.many?
    raise "Multiple instances were declared for model :#{@name}."+
          "Use <#{@name.to_s.pluralize}> to access them"
  end
  self
end

#zip_merge(*hashes) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/active_factory.rb', line 205

def zip_merge *hashes
  @entries.size == hashes.size or raise

  @entries.zip(hashes) { |entry, hash|
    entry.merge hash
  }
  self
end