Class: ROM::Changeset::Associated
- Inherits:
-
Object
- Object
- ROM::Changeset::Associated
- Extended by:
- Initializer
- Defined in:
- lib/rom/repository/changeset/associated.rb
Overview
Associated changesets automatically set up FKs
Instance Attribute Summary collapse
-
#associations ⇒ Array
readonly
List of association identifiers from relation schema.
-
#left ⇒ Changeset::Create
readonly
Child changeset.
Class Method Summary collapse
-
.infer_assoc_name(other) ⇒ Object
private
Infer association name from an object with a schema.
Instance Method Summary collapse
- #associate(other, name = Associated.infer_assoc_name(other)) ⇒ Object
-
#command ⇒ ROM::Command::Composite
Create a composed command.
-
#commit ⇒ Array<Hash>, Hash
Commit changeset’s composite command.
- #relation ⇒ Object private
Instance Attribute Details
#associations ⇒ Array (readonly)
Returns List of association identifiers from relation schema.
17 |
# File 'lib/rom/repository/changeset/associated.rb', line 17 option :associations |
#left ⇒ Changeset::Create (readonly)
Returns Child changeset.
13 |
# File 'lib/rom/repository/changeset/associated.rb', line 13 param :left |
Class Method Details
.infer_assoc_name(other) ⇒ 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.
Infer association name from an object with a schema
This expects other to be an object with a schema that includes a primary key attribute with :source meta information. This makes it work with both structs and relations
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rom/repository/changeset/associated.rb', line 28 def self.infer_assoc_name(other) schema = other.class.schema attrs = schema.is_a?(Hash) ? schema.values : schema pk = attrs.detect { |attr| attr.[:primary_key] } if pk pk.[:source].relation else raise ArgumentError, "can't infer association name for #{other}" end end |
Instance Method Details
#associate(other, name = Associated.infer_assoc_name(other)) ⇒ Object
57 58 59 |
# File 'lib/rom/repository/changeset/associated.rb', line 57 def associate(other, name = Associated.infer_assoc_name(other)) self.class.new(left, associations: associations.merge(name => other)) end |
#command ⇒ ROM::Command::Composite
Create a composed command
This works only with parent => child(ren) changeset hierarchy
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rom/repository/changeset/associated.rb', line 81 def command associations.reduce(left.command.curry(left)) do |a, (assoc, other)| case other when Changeset a >> other.command.with_association(assoc).curry(other) when Associated a >> other.command.with_association(assoc) else a.with_association(assoc, parent: other) end end end |
#commit ⇒ Array<Hash>, Hash
Commit changeset’s composite command
52 53 54 |
# File 'lib/rom/repository/changeset/associated.rb', line 52 def commit command.call end |
#relation ⇒ 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.
95 96 97 |
# File 'lib/rom/repository/changeset/associated.rb', line 95 def relation left.relation end |