Module: DataFactory::BaseFactory

Included in:
Base
Defined in:
lib/data_factory/base_factory.rb

Instance Method Summary collapse

Instance Method Details

#build(params = Hash.new) ⇒ Object

Builds and returns a new instance of a DataFactory class, but does not insert it into the database. For example:

obj = Employee.build(:last_name => ‘Smith’)



34
35
36
37
38
# File 'lib/data_factory/base_factory.rb', line 34

def build(params=Hash.new)
  df = self.new
  df.generate_column_data(params)
  df
end

#create(params = Hash.new) ⇒ Object

Same as the create! method, only the transaction is not committed on the database. For example:

obj = Employee.create(:last_name => ‘Smith’)



23
24
25
26
27
28
# File 'lib/data_factory/base_factory.rb', line 23

def create(params=Hash.new)
  df = build(params)
  df.generate_insert
  df.run_insert
  df
end

#create!(params = Hash.new) ⇒ Object

Builds a new instance of a DataFactory class, inserts it into the database and commits the transaction. The params parameter is expected to be a hash, mapping columns in the underlying table to values. These values will be merged with any defaults set for the columns, and will override the defaults if passed. For example:

obj = Employee.create!(:last_name => ‘Smith’)



13
14
15
16
17
# File 'lib/data_factory/base_factory.rb', line 13

def create!(params=Hash.new)
  df = create(params)
  df.commit
  df
end