Module: Factory

Defined in:
lib/mini_factory.rb

Class Method Summary collapse

Class Method Details

.attributes_for(name, attributes = {}) ⇒ Object



8
9
10
# File 'lib/mini_factory.rb', line 8

def self.attributes_for(name, attributes = {})
  @@factories[name.to_sym].merge(attributes)
end

.build(name, attributes = {}) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/mini_factory.rb', line 12

def self.build(name, attributes = {})   
  obj = Kernel.const_get(name.to_s.capitalize).new()
  self.attributes_for(name, attributes).each do |k, v|
    obj.send(:"#{k}=", v)  
  end
  obj
end

.create(name, attributes = {}) ⇒ Object



20
21
22
23
24
# File 'lib/mini_factory.rb', line 20

def self.create(name, attributes = {})
  obj = self.build(name, attributes)
  obj.save!
  obj
end

.define(name, &block) ⇒ Object



3
4
5
6
# File 'lib/mini_factory.rb', line 3

def self.define(name, &block)
  @@factories ||= {}
  @@factories[name.to_sym] = block.call
end