Method: Satisfactory::Record#with

Defined in:
lib/satisfactory/record.rb

#with(count = nil, downstream_type, force: false, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection

Add an associated record to this record’s build plan.

Parameters:

  • count (Integer) (defaults to: nil)

    The number of records to create.

  • downstream_type (Symbol)

    The type of record to create.

  • force (Boolean) (defaults to: false)

    Whether to force the creation of the record. For internal use only. Use #and instead.

  • attributes (Hash)

    The attributes to use when creating the record.

Returns:

Since:

  • 0.2.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/satisfactory/record.rb', line 43

def with(count = nil, downstream_type, force: false, **attributes) # rubocop:disable Style/OptionalArguments, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
  if singular?(downstream_type)
    if count && count > 1 # rubocop:disable Style/IfUnlessModifier
      raise ArgumentError, "Cannot create multiple of singular associations (e.g. belongs_to)"
    end

    add_singular_association(downstream_type, factory_name: downstream_type, force:, attributes:)
  elsif plural?(downstream_type) && (singular = singular_from_plural(downstream_type))
    add_plural_association(downstream_type, factory_name: singular, count:, force:, attributes:)
  elsif (config = Satisfactory.factory_configurations[downstream_type])
    singular = config[:parent] || downstream_type
    plural = plural_from_singular(singular)
    add_singular_for_plural_association(plural, singular:, factory_name: downstream_type, force:, attributes:)
  elsif (config = Satisfactory.factory_configurations[downstream_type.to_s.singularize])
    unless (parent = config[:parent])
      raise ArgumentError, "Cannot create multiple of singular associations (e.g. belongs_to)"
    end

    plural = plural_from_singular(parent)
    add_plural_association(plural, factory_name: downstream_type.to_s.singularize, count:, force:, attributes:)
  else
    raise ArgumentError, "Unknown association #{type}->#{downstream_type}"
  end
end