Module: ROM::SQL::Plugin::Associates::InstanceMethods Private

Defined in:
lib/rom/sql/plugin/associates.rb

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

Instance Method Summary collapse

Instance Method Details

#__registry__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.



176
177
178
# File 'lib/rom/sql/plugin/associates.rb', line 176

def __registry__
  relation.__registry__
end

#associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent) ⇒ Array<Hash>

Set fk on tuples from parent tuple

Parameters:

  • tuples (Array<Hash>, Hash)

    The input tuple(s)

  • parent (Hash) (defaults to: curried_parent)

    The parent tuple with its pk already set

Returns:

  • (Array<Hash>)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rom/sql/plugin/associates.rb', line 131

def associate(tuples, curried_parent = nil, assoc:, keys:, parent: curried_parent)
  result_type = result

  output_tuples =
    case assoc
    when Symbol
      fk, pk = keys

      with_input_tuples(tuples).map { |tuple|
        tuple.merge(fk => parent.fetch(pk))
      }
    when Association::ManyToMany
      result_type = tuples.is_a?(Array) ? :many : :one

      assoc.persist(__registry__, tuples, parent)

      pk, fk = assoc.parent_combine_keys(__registry__)

      case parent
      when Array
        parent.map do |p|
          tuples.map { |tuple| Hash(tuple).merge(fk => p[pk]) }
        end.flatten(1)
      else
        tuples.map { |tuple| Hash(tuple).update(fk => parent[pk]) }
      end
    when Association
      with_input_tuples(tuples).map { |tuple|
        assoc.associate(__registry__, tuple, parent)
      }
    end

  result_type == :one ? output_tuples[0] : output_tuples
end

#with_association(name, opts = EMPTY_HASH) ⇒ Object



167
168
169
170
171
172
173
# File 'lib/rom/sql/plugin/associates.rb', line 167

def with_association(name, opts = EMPTY_HASH)
  self.class.build(
    relation,
    **options,
    associations: associations.merge(name => opts)
  )
end