Class: Satisfactory::Record
- Inherits:
-
Object
- Object
- Satisfactory::Record
- Defined in:
- lib/satisfactory/record.rb
Overview
This whole class needs a tidy up
Represents a usage of a type.
Instance Attribute Summary collapse
- #attributes ⇒ Object private
- #factory_name ⇒ Object private
- #traits ⇒ Object private
- #type ⇒ Object private
- #type_config ⇒ Object private
- #upstream ⇒ Object private
Instance Method Summary collapse
-
#and(count = nil, downstream_type, **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(count = nil, downstream_type, force: false, **attributes) ⇒ Satisfactory::Record, Satisfactory::Collection
Add an associated record to this record’s build plan.
-
#with_new(count = nil, downstream_type, **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.
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
#attributes ⇒ 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.
33 34 35 |
# File 'lib/satisfactory/record.rb', line 33 def attributes @attributes end |
#factory_name ⇒ 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.
33 34 35 |
# File 'lib/satisfactory/record.rb', line 33 def factory_name @factory_name end |
#traits ⇒ 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.
33 34 35 |
# File 'lib/satisfactory/record.rb', line 33 def traits @traits end |
#type ⇒ 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.
33 34 35 |
# File 'lib/satisfactory/record.rb', line 33 def type @type end |
#type_config ⇒ 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.
33 34 35 |
# File 'lib/satisfactory/record.rb', line 33 def type_config @type_config end |
#upstream ⇒ 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.
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.
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.
99 100 101 |
# File 'lib/satisfactory/record.rb', line 99 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.
136 137 138 |
# File 'lib/satisfactory/record.rb', line 136 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.
128 129 130 131 132 |
# File 'lib/satisfactory/record.rb', line 128 def build_plan { traits:, }.merge(associations_plan).compact_blank end |
#create ⇒ Hash<Symbol, Array<ApplicationRecord>>
Check if we still need the upstream check.
Trigger the creation of this tree’s build plan.
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_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.
142 143 144 |
# File 'lib/satisfactory/record.rb', line 142 def create_self reify(:create) end |
#to_plan ⇒ Hash
Construct this tree’s build plan.
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.
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.
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.
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 |