Class: Satisfactory::Record

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

Overview

TODO:

This whole class needs a tidy up

Represents a usage of a type.

Since:

  • 0.2.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, factory_name: nil, upstream: nil, attributes: {}) ⇒ Record

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Record.

Parameters:

  • type (Symbol)

    The type of record to create. Must be a known factory.

  • factory_name (Symbol) (defaults to: nil)

    The name of the factory to use (if different).

  • upstream (Satisfactory::Record, Satisfactory::Collection, Satisfactory::Root) (defaults to: nil)

    The upstream record-ish.

  • attributes (Hash) (defaults to: {})

    The attributes to use when creating the record.

Raises:

  • (ArgumentError)

Since:

  • 0.2.0



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/satisfactory/record.rb', line 14

def initialize(type:, factory_name: nil, upstream: nil, attributes: {})
  @factory_name = factory_name || type

  config = Satisfactory.factory_configurations[type]
  raise ArgumentError, "Unknown factory #{type}" unless config

  @type = config[:parent] || type
  @type_config = Satisfactory.factory_configurations[@type]
  @traits = []
  @upstream = upstream

  @attributes = attributes

  @associations = type_config.dig(:associations, :plural).each.with_object({}) do |name, hash|
    hash[name] = Satisfactory::Collection.new(upstream: self)
  end
end

Instance Attribute Details

#attributesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def attributes
  @attributes
end

#factory_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def factory_name
  @factory_name
end

#traitsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def traits
  @traits
end

#typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def type
  @type
end

#type_configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def type_config
  @type_config
end

#upstreamObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



33
34
35
# File 'lib/satisfactory/record.rb', line 33

def upstream
  @upstream
end

Instance Method Details

#and(count = nil, downstream_type, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection

Add a sibling record to the parent record’s build plan. e.g. adding a second user to a project.

Parameters:

  • count (Integer) (defaults to: nil)

    The number of records to create.

  • downstream_type (Symbol)

    The type of record to create.

  • attributes (Hash)

    The attributes to use when creating the record.

Returns:

Since:

  • 0.2.0



83
84
85
# File 'lib/satisfactory/record.rb', line 83

def and(count = nil, downstream_type, **attributes) # rubocop:disable Style/OptionalArguments
  upstream.with(count, downstream_type, force: true, **attributes)
end

#and_same(upstream_type) ⇒ Satisfactory::Record, ... Also known as: return_to

Locate the nearest ancestor of the given type.

Parameters:

  • upstream_type (Symbol)

    The type of ancestor to find.

Returns:

Since:

  • 0.2.0



99
100
101
# File 'lib/satisfactory/record.rb', line 99

def and_same(upstream_type)
  Satisfactory::UpstreamRecordFinder.new(upstream:).find(upstream_type)
end

#buildApplicationRecord

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (ApplicationRecord)

Since:

  • 0.2.0



136
137
138
# File 'lib/satisfactory/record.rb', line 136

def build
  reify(:build)
end

#build_planObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.0



128
129
130
131
132
# File 'lib/satisfactory/record.rb', line 128

def build_plan
  {
    traits:,
  }.merge(associations_plan).compact_blank
end

#createHash<Symbol, Array<ApplicationRecord>>

TODO:

Check if we still need the upstream check.

Trigger the creation of this tree’s build plan.

Returns:

  • (Hash<Symbol, Array<ApplicationRecord>>)

Since:

  • 0.2.0



108
109
110
111
112
113
114
# File 'lib/satisfactory/record.rb', line 108

def create
  if upstream
    upstream.create
  else
    create_self
  end
end

#create_selfApplicationRecord

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (ApplicationRecord)

Since:

  • 0.2.0



142
143
144
# File 'lib/satisfactory/record.rb', line 142

def create_self
  reify(:create)
end

#to_planHash

Construct this tree’s build plan.

Returns:

  • (Hash)

Since:

  • 0.2.0



119
120
121
122
123
124
125
# File 'lib/satisfactory/record.rb', line 119

def to_plan
  if upstream
    upstream.to_plan
  else
    build_plan
  end
end

#which_is(*traits) ⇒ Object

Apply one or more traits to this record’s build plan.

Parameters:

  • traits (Symbol, ...)

    The traits to apply.

Since:

  • 0.2.0



90
91
92
93
# File 'lib/satisfactory/record.rb', line 90

def which_is(*traits)
  traits.each { |trait| self.traits << trait }
  self
end

#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

#with_new(count = nil, downstream_type, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection

Same as #with but always creates a new record.

Parameters:

  • count (Integer) (defaults to: nil)

    The number of records to create.

  • downstream_type (Symbol)

    The type of record to create.

  • attributes (Hash)

    The attributes to use when creating the record.

Returns:

Since:

  • 0.2.0



72
73
74
# File 'lib/satisfactory/record.rb', line 72

def with_new(count = nil, downstream_type, **attributes) # rubocop:disable Style/OptionalArguments
  with(count, downstream_type, force: true, **attributes)
end