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

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

Set fk on tuples from parent tuple

rubocop:disable Lint/UnusedMethodArgument

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


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rom/sql/plugin/associates.rb', line 149

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

  output_tuples =
    case assoc
    when SQL::Associations::ManyToMany
      result_type = tuples.is_a?(Array) ? :many : :one

      assoc.persist(tuples, parent)

      pk, fk = assoc.parent_combine_keys

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

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

#with_association(name, opts = EMPTY_HASH) ⇒ Command

Return a new command with the provided association

Parameters:

  • name (Symbol, Relation::Name)

    The name of the association

Returns:

  • (Command)


186
187
188
189
190
191
# File 'lib/rom/sql/plugin/associates.rb', line 186

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