Method: ObjectDaddy::ClassMethods#spawn

Defined in:
lib/object_daddy.rb

#spawn(args = {}) {|instance| ... } ⇒ Object

:call-seq:

spawn()
spawn() do |obj| ... end
spawn(args)
spawn(args) do |obj| ... end

Creates a valid instance of this class, using any known generators. The generated instance is yielded to a block if provided.

Yields:

  • (instance)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/object_daddy.rb', line 28

def spawn(args = {})
  gather_exemplars
  if @concrete_subclass_name
    return block_given? \
      ? const_get(@concrete_subclass_name).spawn(args) {|instance| yield instance} \
      : const_get(@concrete_subclass_name).spawn(args)
  end
  generate_values(args)
  instance = new
  args.each_pair do |attribute, value|
    instance.send("#{attribute}=", value)  # support setting of mass-assignment protected attributes
  end
  yield instance if block_given?
  instance
end