Class: ActiveFactory::Container
- Inherits:
-
Object
- Object
- ActiveFactory::Container
- Defined in:
- lib/active_factory.rb
Overview
keeps collection of created instances of the given model class
Instance Attribute Summary collapse
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#factory ⇒ Object
readonly
Returns the value of attribute factory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #attrs ⇒ Object
- #before_save ⇒ Object
- #build ⇒ Object
- #create(count) ⇒ Object
-
#initialize(name, factory, context) ⇒ Container
constructor
A new instance of Container.
- #make_linker ⇒ Object
- #objects ⇒ Object
- #save ⇒ Object
- #singleton ⇒ Object
- #zip_merge(*hashes) ⇒ Object
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
#entries ⇒ Object
Returns the value of attribute entries.
181 182 183 |
# File 'lib/active_factory.rb', line 181 def entries @entries end |
#factory ⇒ Object (readonly)
Returns the value of attribute factory.
182 183 184 |
# File 'lib/active_factory.rb', line 182 def factory @factory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
182 183 184 |
# File 'lib/active_factory.rb', line 182 def name @name end |
Instance Method Details
#attrs ⇒ Object
231 232 233 |
# File 'lib/active_factory.rb', line 231 def attrs @entries.map &:attrs end |
#before_save ⇒ Object
223 224 225 |
# File 'lib/active_factory.rb', line 223 def before_save @entries.each &:before_save end |
#build ⇒ Object
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_linker ⇒ Object
219 220 221 |
# File 'lib/active_factory.rb', line 219 def make_linker Linker.new self end |
#objects ⇒ Object
235 236 237 |
# File 'lib/active_factory.rb', line 235 def objects @entries.map &:model end |
#save ⇒ Object
227 228 229 |
# File 'lib/active_factory.rb', line 227 def save @entries.each &:save end |
#singleton ⇒ Object
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 |