Class: Object::Factory

Inherits:
Object show all
Defined in:
lib/object_factory.rb

Overview

Factory allows test suites to build new instances of objects, specifying some simple constraints on certain fields If a new instance is created via the factory then that instance can have specialist values automatically applied to given fields, meaning that it should be possible for test cases to build valid objects without having to specify a full valid field-set The factory should not be created directly, but instead accessed through the Object#factory method.

Expected usage:

Object.factory.configure Person, :auto_generate => [:email, :telephone], :auto_confirm => :password
instance = a Person

instance will have a unique value for :email and :telephone and will ensure that :password and :password_confirmation have the same value.

Defined Under Namespace

Classes: CannotSaveError, ValueGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFactory

Returns a new instance of Factory.



20
21
22
# File 'lib/object_factory.rb', line 20

def initialize
  reset
end

Instance Attribute Details

#generatorObject

An Object::Factory::ValueGenerator that is used to actually build the unique values used to populate the required fields



79
80
81
# File 'lib/object_factory.rb', line 79

def generator
  @generator
end

Instance Method Details

#clean_upObject

clean up all instances - all classes that are registered for clean up have all instances deleted this is useful if you cannot use transactions to tidy up after each test



33
34
35
36
37
# File 'lib/object_factory.rb', line 33

def clean_up
  @classes_for_clean_up.each do | klass | 
    klass.delete_all
  end
end

#create_a(klass, parameters = {}) ⇒ Object

Create a new instance of the given class with the given parameters and apply the auto-generated fields, according to the configured rules



40
41
42
43
44
45
46
47
# File 'lib/object_factory.rb', line 40

def create_a klass, parameters = {}
  instance = klass.new parameters
  
  generate_confirmations_for instance, parameters
  generate_values_for instance, parameters
  
  return instance
end

#create_and_save_a(klass, parameters = {}) ⇒ Object Also known as: create_and_save_an

Create a new instance of the given class with the given parameters, auto-generate the field values and then call save!

Raises:



50
51
52
53
54
# File 'lib/object_factory.rb', line 50

def create_and_save_a klass, parameters = {}
  instance = create_a klass, parameters
  raise CannotSaveError, instance.errors.inspect unless instance.save
  return instance
end

#next_numberObject

Generate a unique Integer



84
85
86
# File 'lib/object_factory.rb', line 84

def next_number
  generator.unique_integer
end

print the rules for a given class



107
108
109
110
111
112
113
114
# File 'lib/object_factory.rb', line 107

def print_configuration_for klass
  fields_and_generators = @generators[symbol_for(klass)]
  unless fields_and_generators.nil? 
    fields_and_generators.each do | field_name, generator | 
      puts "#{field_name} uses a lambda"
    end
  end
end

#resetObject

Set this factory back to its pristine state, with no objects configured



25
26
27
28
29
# File 'lib/object_factory.rb', line 25

def reset
  @confirmed_fields = {}
  @generators = {}
  @classes_for_clean_up = []
end

#when_creating_a(klass, options = {}) ⇒ Object Also known as: when_creating_an

Set up the required auto-generated fields for the given class.

Object.factory.when_creating_a MyClass, :auto_generate => [:field1, :field2], :auto_confirm => :password, :generate_email_address => :email_address, :set => { :field3 => 'value3', :field4 => 'value4' }, :generator => { :field5 => lambda {Date.today}, :field6 => lambda {Time.now} }

Options are:

  • :auto_generate specifies a field name or array of field names that are to have unique string values assigned to them

  • :auto_confirm specifies a field name or array of field names that are to be set to a unique value; with the same value being assigned to field_name_confirmation

  • :generate_email_address specifies a field name or array of field names that are set to be randomised email addresses

  • :set specifies a Hash of field names and fixed values

  • :generate specifies a Hash of field names and lambdas that are used to generate a dynamic value

  • :clean_up specifies whether the class should be registered for clean up (the default is true)



65
66
67
68
69
70
71
72
73
# File 'lib/object_factory.rb', line 65

def when_creating_a klass, options = {}
  need_to_generate_values_for klass, options[:auto_generate] unless options[:auto_generate].nil?
  need_to_confirm_values_for klass, options[:auto_confirm] unless options[:auto_confirm].nil? 
  need_to_generate_email_addresses_for klass, options[:generate_email_address] unless options[:generate_email_address].nil?
  need_to_generate_ip_addresses_for klass, options[:generate_ip_address] unless options[:generate_ip_address].nil?
  need_to_set_values_for klass, options[:set] unless options[:set].nil? 
  need_to_set_generators_for klass, options[:generate] unless options[:generate].nil?
  register_for_clean_up klass unless options[:clean_up] == false
end