Module: ObjectDaddy::ClassMethods

Defined in:
lib/object_daddy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#exemplar_pathObject

Returns the value of attribute exemplar_path.



16
17
18
# File 'lib/object_daddy.rb', line 16

def exemplar_path
  @exemplar_path
end

#exemplars_generatedObject

Returns the value of attribute exemplars_generated.



16
17
18
# File 'lib/object_daddy.rb', line 16

def exemplars_generated
  @exemplars_generated
end

#generatorsObject

Returns the value of attribute generators.



16
17
18
# File 'lib/object_daddy.rb', line 16

def generators
  @generators
end

#presence_validated_attributesObject (readonly)

Returns the value of attribute presence_validated_attributes.



17
18
19
# File 'lib/object_daddy.rb', line 17

def presence_validated_attributes
  @presence_validated_attributes
end

Instance Method Details

#gather_exemplarsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/object_daddy.rb', line 94

def gather_exemplars
  return if exemplars_generated
  
  self.generators ||= {}
  if superclass.respond_to?(:gather_exemplars)
    superclass.gather_exemplars
    self.generators = (superclass.generators).merge(generators).dup
  end

  exemplar_path.each do |raw_path|
    path = File.join(raw_path, "#{underscore(name)}_exemplar.rb")
    load(path) if File.exists?(path)
  end
  
  self.exemplars_generated = true
end

#generates_subclass(subclass_name) ⇒ Object



90
91
92
# File 'lib/object_daddy.rb', line 90

def generates_subclass(subclass_name)
  @concrete_subclass_name = subclass_name.to_s
end

#generator_for(handle, args = {}, &block) ⇒ Object

register a generator for an attribute of this class generator_for :foo do |prev| … end generator_for :foo do … end generator_for :foo, value generator_for :foo => value generator_for :foo, :class => GeneratorClass generator_for :foo, :method => :method_name

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/object_daddy.rb', line 51

def generator_for(handle, args = {}, &block)
  if handle.is_a?(Hash)
    raise ArgumentError, "only specify one attr => value pair at a time" unless handle.keys.length == 1
    gen_data = handle
    handle = gen_data.keys.first
    args = gen_data[handle]
  end
  
  raise ArgumentError, "an attribute name must be specified" unless handle = handle.to_sym
  
  unless args.is_a?(Hash)
    unless block
      retval = args
      block = lambda { retval }  # lambda { args } results in returning the empty hash that args gets changed to
    end
    args = {}  # args is assumed to be a hash for the rest of the method
  end
  
  if args[:start]
    block ||= lambda { |prev|  prev.succ }
  end
  
  if args[:method]
    h = { :method => args[:method].to_sym }
    h[:start] = args[:start] if args[:start]
    record_generator_for(handle, h)
  elsif args[:class]
    raise ArgumentError, "generator class [#{args[:class].name}] does not have a :next method" unless args[:class].respond_to?(:next)
    record_generator_for(handle, :class => args[:class])
  elsif block
    raise ArgumentError, "generator block must take an optional single argument" unless (-1..1).include?(block.arity)  # NOTE: lambda {} has an arity of -1, while lambda {||} has an arity of 0
    h = { :block => block }
    h[:start] = args[:start] if args[:start]
    record_generator_for(handle, h)
  else
    raise ArgumentError, "a block, :class generator, :method generator, or value must be specified to generator_for"
  end
end

#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