Class: FactoryBot::With

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Attribute Details

#attrs{Symbol => Object} (readonly)

Returns:

  • ({Symbol => Object})


24
25
26
# File 'lib/factory_bot/with.rb', line 24

def attrs
  @attrs
end

#blockProc? (readonly)

Returns:

  • (Proc, nil)


26
27
28
# File 'lib/factory_bot/with.rb', line 26

def block
  @block
end

#factory_nameSymbol (readonly)

Returns:

  • (Symbol)


18
19
20
# File 'lib/factory_bot/with.rb', line 18

def factory_name
  @factory_name
end

#traitsArray<Numeric, Symbol> (readonly)

Returns Numeric is also treated as a trait for convenience.

Returns:

  • (Array<Numeric, Symbol>)

    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)

Returns:

  • (:singular, :pair, :list)


16
17
18
# File 'lib/factory_bot/with.rb', line 16

def variation
  @variation
end

#withesArray<With> (readonly)

Returns:



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.

Examples:

FactoryBot.register_strategy(:json, JsonStrategy)
FactoryBot::With.register_strategy(:json)

Parameters:

  • build_strategy (Symbol)


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