Class: Satisfactory::Record

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

Overview

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



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

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 (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def attributes
  @attributes
end

#factory_nameObject (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def factory_name
  @factory_name
end

#traitsObject (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def traits
  @traits
end

#typeObject (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def type
  @type
end

#type_configObject (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def type_config
  @type_config
end

#upstreamObject (readonly)

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



35
36
37
# File 'lib/satisfactory/record.rb', line 35

def upstream
  @upstream
end

Instance Method Details

#and(**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)

    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



75
76
77
# File 'lib/satisfactory/record.rb', line 75

def and(*, **attributes)
  upstream.with(*, 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



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

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



127
128
129
# File 'lib/satisfactory/record.rb', line 127

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



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

def build_plan
  plan = associations_plan
  plan[:traits] = traits if traits.any?
  plan.merge(attributes)
end

#createHash<Symbol, Array<ApplicationRecord>>

Trigger the creation of this tree’s build plan.

Returns:

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

Since:

  • 0.2.0



99
100
101
102
103
104
105
# File 'lib/satisfactory/record.rb', line 99

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



133
134
135
# File 'lib/satisfactory/record.rb', line 133

def create_self
  reify(:create)
end

#to_planHash

Construct this tree’s build plan.

Returns:

  • (Hash)

Since:

  • 0.2.0



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

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



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

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

#with(*arguments, force: false, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection

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

Parameters:

  • count (Integer)

    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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/satisfactory/record.rb', line 45

def with(*arguments, force: false, **attributes)
  count, downstream_type = parse_with_arguments(arguments)

  case association_kind(downstream_type)
  when :singular then add_singular(downstream_type, count:, force:, attributes:)
  when :plural then add_plural(downstream_type, count:, force:, attributes:)
  when :child then add_child(downstream_type, force:, attributes:)
  when :child_collection then add_child_collection(downstream_type, count:, force:, attributes:)
  when :singular_collection
    raise ArgumentError, "Cannot create multiple of singular associations (e.g. belongs_to)"
  else
    raise ArgumentError, "Unknown association #{type}->#{downstream_type}"
  end
end

#with_new(**attributes) ⇒ Satisfactory::Record, Satisfactory::Collection

Same as #with but always creates a new record.

Parameters:

  • count (Integer)

    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



64
65
66
# File 'lib/satisfactory/record.rb', line 64

def with_new(*, **attributes)
  with(*, force: true, **attributes)
end