Class: Satisfactory::Record
- Inherits:
-
Object
- Object
- Satisfactory::Record
- Defined in:
- lib/satisfactory/record.rb
Overview
Represents a usage of a type.
Instance Attribute Summary collapse
- #attributes ⇒ Object readonly private
- #factory_name ⇒ Object readonly private
- #traits ⇒ Object readonly private
- #type ⇒ Object readonly private
- #type_config ⇒ Object readonly private
- #upstream ⇒ Object readonly private
Instance Method Summary collapse
-
#and(**attributes) ⇒ Satisfactory::Record, Satisfactory::Collection
Add a sibling record to the parent record’s build plan.
-
#and_same(upstream_type) ⇒ Satisfactory::Record, ...
(also: #return_to)
Locate the nearest ancestor of the given type.
- #build ⇒ ApplicationRecord private
- #build_plan ⇒ Object private
-
#create ⇒ Hash<Symbol, Array<ApplicationRecord>>
Trigger the creation of this tree’s build plan.
- #create_self ⇒ ApplicationRecord private
-
#initialize(type:, factory_name: nil, upstream: nil, attributes: {}) ⇒ Record
constructor
private
A new instance of Record.
-
#to_plan ⇒ Hash
Construct this tree’s build plan.
-
#which_is(*traits) ⇒ Object
Apply one or more traits to this record’s build plan.
-
#with(*arguments, force: false, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection
Add an associated record to this record’s build plan.
-
#with_new(**attributes) ⇒ Satisfactory::Record, Satisfactory::Collection
Same as #with but always creates a new record.
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.
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
#attributes ⇒ Object (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.
35 36 37 |
# File 'lib/satisfactory/record.rb', line 35 def attributes @attributes end |
#factory_name ⇒ Object (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.
35 36 37 |
# File 'lib/satisfactory/record.rb', line 35 def factory_name @factory_name end |
#traits ⇒ Object (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.
35 36 37 |
# File 'lib/satisfactory/record.rb', line 35 def traits @traits end |
#type ⇒ Object (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.
35 36 37 |
# File 'lib/satisfactory/record.rb', line 35 def type @type end |
#type_config ⇒ Object (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.
35 36 37 |
# File 'lib/satisfactory/record.rb', line 35 def type_config @type_config end |
#upstream ⇒ Object (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.
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.
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.
91 92 93 |
# File 'lib/satisfactory/record.rb', line 91 def and_same(upstream_type) Satisfactory::UpstreamRecordFinder.new(upstream:).find(upstream_type) end |
#build ⇒ ApplicationRecord
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.
127 128 129 |
# File 'lib/satisfactory/record.rb', line 127 def build reify(:build) end |
#build_plan ⇒ Object
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.
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 |
#create ⇒ Hash<Symbol, Array<ApplicationRecord>>
Trigger the creation of this tree’s build plan.
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_self ⇒ ApplicationRecord
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.
133 134 135 |
# File 'lib/satisfactory/record.rb', line 133 def create_self reify(:create) end |
#to_plan ⇒ Hash
Construct this tree’s build plan.
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.
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.
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.
64 65 66 |
# File 'lib/satisfactory/record.rb', line 64 def with_new(*, **attributes) with(*, force: true, **attributes) end |