Class: FactoryBot::With
- Inherits:
-
Object
- Object
- FactoryBot::With
- Extended by:
- Methods
- Defined in:
- lib/factory_bot/with.rb,
lib/factory_bot/with/proxy.rb,
lib/factory_bot/with/scoped.rb,
lib/factory_bot/with/methods.rb,
lib/factory_bot/with/version.rb,
lib/factory_bot/with/assoc_info.rb
Overview
An intermediate state for with
operator.
Defined Under Namespace
Modules: Methods Classes: AssocInfo, Proxy, Scoped
Constant Summary collapse
- VERSION =
"0.6.0"
Instance Attribute Summary collapse
- #attrs ⇒ {Symbol => Object} readonly
- #block ⇒ Proc? readonly
- #factory_name ⇒ Symbol readonly
-
#traits ⇒ Array<Numeric, Symbol>
readonly
Numeric is also treated as a trait for convenience.
- #variation ⇒ :singular, ... readonly
- #withes ⇒ Array<With> readonly
Class Method Summary collapse
-
.register_strategy(build_strategy) ⇒ Object
If you want to use a custom strategy, call this along with
FactoryBot.register_strategy
.
Instance Attribute Details
#attrs ⇒ {Symbol => Object} (readonly)
24 25 26 |
# File 'lib/factory_bot/with.rb', line 24 def attrs @attrs end |
#block ⇒ Proc? (readonly)
26 27 28 |
# File 'lib/factory_bot/with.rb', line 26 def block @block end |
#factory_name ⇒ Symbol (readonly)
18 19 20 |
# File 'lib/factory_bot/with.rb', line 18 def factory_name @factory_name end |
#traits ⇒ Array<Numeric, Symbol> (readonly)
Returns Numeric is also treated as a trait for convenience.
22 23 24 |
# File 'lib/factory_bot/with.rb', line 22 def traits @traits end |
#variation ⇒ :singular, ... (readonly)
16 17 18 |
# File 'lib/factory_bot/with.rb', line 16 def variation @variation end |
#withes ⇒ Array<With> (readonly)
20 21 22 |
# File 'lib/factory_bot/with.rb', line 20 def withes @withes end |
Class Method Details
.register_strategy(build_strategy) ⇒ Object
If you want to use a custom strategy, call this along with FactoryBot.register_strategy
.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/factory_bot/with.rb', line 141 def register_strategy(build_strategy) { singular: build_strategy, pair: :"#{build_strategy}_pair", list: :"#{build_strategy}_list", }.each do |variation, method_name| Methods.define_method(method_name) do |factory = nil, *args, **kwargs, &block| if factory.is_a? With # <__method__>(<with_instance>, ...) factory .merge(With.new(variation, factory.factory_name, *args, attrs: kwargs, &block)) .instantiate(build_strategy) elsif factory # <__method__>(<factory_name>, ...) With.new(variation, factory, *args, attrs: kwargs, &block).instantiate(build_strategy) elsif args.empty? && kwargs.empty? && !block # <__method__>.<factory_name>(...) Proxy.new(self, __method__) elsif __method__ == :with && args.empty? && !kwargs.empty? && block # with(<factory_name>: <object>, ...) { ... } Scoped.block(&block).call(kwargs) elsif __method__ == :with_list && args.empty? && !kwargs.empty? && block # with_list(<factory_name>: [<object>, ...], ...) { ... } block = Scoped.block(&block) kwargs.values.inject(:product).map { block.call(kwargs.keys.zip(_1).to_h) } else raise ArgumentError, "Invalid use of #{__method__}" end end end end |