Class: Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/factory_hero/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol, options = {}, &block) ⇒ Factory

Returns a new instance of Factory.



5
6
7
8
9
10
11
# File 'lib/factory_hero/factory.rb', line 5

def initialize symbol, options = {}, &block
  @symbol = symbol
  @default_attributes  = {}
  @options = options

  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

used to assign default attributes



21
22
23
# File 'lib/factory_hero/factory.rb', line 21

def method_missing(method, *args)
  @default_attributes[method] = args.first
end

Instance Attribute Details

#default_attributesObject (readonly)

Returns the value of attribute default_attributes.



3
4
5
# File 'lib/factory_hero/factory.rb', line 3

def default_attributes
  @default_attributes
end

#symbolObject (readonly)

Returns the value of attribute symbol.



3
4
5
# File 'lib/factory_hero/factory.rb', line 3

def symbol
  @symbol
end

Instance Method Details

#build(attributes = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/factory_hero/factory.rb', line 13

def build attributes = {}
  build_object.tap do |obj|
    assign_attributes default_attributes
    assign_attributes attributes
  end
end