Class: Cranky::FactoryBase
- Inherits:
-
Object
- Object
- Cranky::FactoryBase
- Defined in:
- lib/cranky/factory.rb
Direct Known Subclasses
Constant Summary collapse
- TRAIT_METHOD_REGEXP =
/apply_trait_(\w+)_to_(\w+)/.freeze
Instance Method Summary collapse
- #attributes_for(what, attrs = {}) ⇒ Object
- #build(what, overrides = {}) ⇒ Object
- #create(what, overrides = {}) ⇒ Object
- #create!(what, overrides = {}) ⇒ Object
-
#debug(*args) ⇒ Object
Can be left in your tests as an alternative to build and to warn if your factory method ever starts producing invalid instances.
-
#debug!(*args) ⇒ Object
Same thing for create.
- #factory_names ⇒ Object
- #fetch(*args) ⇒ Object
-
#initialize ⇒ FactoryBase
constructor
A new instance of FactoryBase.
-
#lint!(factory_names: nil, traits: false) ⇒ Object
Look for errors in factories and (optionally) their traits.
-
#reset ⇒ Object
Reset the factory instance, clear all instance variables.
- #traits_for(factory_name) ⇒ Object
Constructor Details
#initialize ⇒ FactoryBase
Returns a new instance of FactoryBase.
6 7 8 9 10 11 12 13 |
# File 'lib/cranky/factory.rb', line 6 def initialize # Factory jobs can be nested, i.e. a factory method can itself invoke another factory method to # build a dependent object. In this case jobs the jobs are pushed into a pipeline and executed # in a last in first out order. @pipeline = [] @n = 0 @errors = [] end |
Instance Method Details
#attributes_for(what, attrs = {}) ⇒ Object
39 40 41 |
# File 'lib/cranky/factory.rb', line 39 def attributes_for(what, attrs={}) build(what, attrs.merge(:_return_attributes => true)) end |
#build(what, overrides = {}) ⇒ Object
15 16 17 |
# File 'lib/cranky/factory.rb', line 15 def build(what, overrides={}) crank_it(what, overrides) end |
#create(what, overrides = {}) ⇒ Object
19 20 21 22 23 |
# File 'lib/cranky/factory.rb', line 19 def create(what, overrides={}) item = build(what, overrides) Array(item).each(&:save) item end |
#create!(what, overrides = {}) ⇒ Object
25 26 27 28 29 |
# File 'lib/cranky/factory.rb', line 25 def create!(what, overrides={}) item = build(what, overrides) Array(item).each(&:save!) item end |
#debug(*args) ⇒ Object
Can be left in your tests as an alternative to build and to warn if your factory method ever starts producing invalid instances
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/cranky/factory.rb', line 45 def debug(*args) item = build(*args) invalid_item = Array(item).find(&:invalid?) if invalid_item if invalid_item.errors.respond_to?(:messages) errors = invalid_item.errors. else errors = invalid_item.errors end raise "Oops, the #{invalid_item.class} created by the Factory has the following errors: #{errors}" end item end |
#debug!(*args) ⇒ Object
Same thing for create
60 61 62 63 64 |
# File 'lib/cranky/factory.rb', line 60 def debug!(*args) item = debug(*args) item.save item end |
#factory_names ⇒ Object
77 78 79 |
# File 'lib/cranky/factory.rb', line 77 def factory_names public_methods(false).reject {|m| TRAIT_METHOD_REGEXP === m } end |
#fetch(*args) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/cranky/factory.rb', line 87 def fetch(*args) if block_given? .fetch(*args, &Proc.new) else .fetch(*args) end end |
#lint!(factory_names: nil, traits: false) ⇒ Object
Look for errors in factories and (optionally) their traits. Parameters: factory_names - which factories to lint; omit for all factories options:
traits : true - to lint traits as well as factories
71 72 73 74 75 |
# File 'lib/cranky/factory.rb', line 71 def lint!(factory_names: nil, traits: false) factories_to_lint = Array(factory_names || self.factory_names) strategy = traits ? :factory_and_traits : :factory Linter.new(self, factories_to_lint, strategy).lint! end |
#reset ⇒ Object
Reset the factory instance, clear all instance variables
32 33 34 35 36 37 |
# File 'lib/cranky/factory.rb', line 32 def reset self.instance_variables.each do |var| instance_variable_set(var, nil) end initialize end |
#traits_for(factory_name) ⇒ Object
81 82 83 84 85 |
# File 'lib/cranky/factory.rb', line 81 def traits_for(factory_name) regexp = /^apply_trait_(\w+)_to_#{factory_name}$/.freeze trait_methods = public_methods(false).select {|m| regexp === m } trait_methods.map {|m| regexp.match(m)[1] } end |