Class: Rubory::Factories

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

Instance Method Summary collapse

Constructor Details

#initialize(default_module = '::') ⇒ Factories

Returns a new instance of Factories.



25
26
27
# File 'lib/factories.rb', line 25

def initialize(default_module='::')
  @default_module = default_module
end

Instance Method Details

#attributes_for(name, attribute_overrides = {}) ⇒ Object



42
43
44
45
# File 'lib/factories.rb', line 42

def attributes_for(name, attribute_overrides = {})
  object = build(name, attribute_overrides)
  object.values || object.attributes || raise(NotSupportedException.new(":attributes_for is not supported as your model does not support :values or :attributes methods"))
end

#build(name, attribute_overrides = {}) ⇒ Object



39
40
41
# File 'lib/factories.rb', line 39

def build(name, attribute_overrides={})
  factory(name).build(attribute_overrides)
end

#create(name, attribute_overrides = {}) ⇒ Object



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

def create(name, attribute_overrides = {})
  object = build(name, attribute_overrides)
  object.save
  object
end

#create_instance(class_name, attributes) ⇒ Object



54
55
56
# File 'lib/factories.rb', line 54

def create_instance(class_name, attributes)
  eval(expand_class(class_name)).new attributes
end

#define(name, &definition) ⇒ Object



31
32
33
# File 'lib/factories.rb', line 31

def define(name, &definition)
  factories[name] = CreationDsl.create_factory(name, self, &definition)
end

#expand_class(class_name) ⇒ Object



57
58
59
60
# File 'lib/factories.rb', line 57

def expand_class(class_name)
  return @default_module + class_name if @default_module == '::'
  [@default_module, class_name].join '::'
end

#factoriesObject



50
51
52
# File 'lib/factories.rb', line 50

def factories
  @factories ||= {}
end

#factory(name) ⇒ Object



46
47
48
# File 'lib/factories.rb', line 46

def factory(name)
  factories[name] || raise(NoSuchFactoryException.new(name.to_s))
end

#use_default_module(default_module) ⇒ Object



28
29
30
# File 'lib/factories.rb', line 28

def use_default_module(default_module)
  @default_module = default_module
end