class Factory

Attributes

default_attributes[R]
object[R]
options[R]
symbol[R]

Public Class Methods

new(symbol, options = {}) click to toggle source
# 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

Public Instance Methods

build(attributes = {}) click to toggle source
# 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
method_missing(method, *args) click to toggle source

used to assign default attributes

# File lib/factory_hero/factory.rb, line 21
def method_missing(method, *args)
  @default_attributes[method] = args.first
end

Private Instance Methods

assign_attributes(attributes) click to toggle source
# File lib/factory_hero/factory.rb, line 45
def assign_attributes attributes
  attributes.each do |attribute, value|
    object.public_send "#{ attribute }=", value
  end
end
build_object() click to toggle source
# File lib/factory_hero/factory.rb, line 29
def build_object
  @object = klass.new
end
class_from_options() click to toggle source
# File lib/factory_hero/factory.rb, line 37
def class_from_options
  options[:class]
end
class_from_symbol() click to toggle source
# File lib/factory_hero/factory.rb, line 41
def class_from_symbol
  Object::const_get symbol.to_s.capitalize
end
klass() click to toggle source
# File lib/factory_hero/factory.rb, line 33
def klass
  @klass ||= class_from_options || class_from_symbol
end