Class: OccamsRecord::Merge

Inherits:
Object
  • Object
show all
Defined in:
lib/occams-record/merge.rb

Overview

Represents a merge operation to be performed. Merges are always “left” merges. You initialize the Merge with the “left” records, and the name of the attribute into which “right” records will be placed.

After initializing, perform a specific type of merge by calling the appropriate *! method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_rows, assoc_attr) ⇒ Merge

Initialize a new Merge operation.

Parameters:

  • target_rows (Array<OccamsRecord::Results::Row] the rows into which associated rows should be merged)

    arget_rows [Array<OccamsRecord::Results::Row] the rows into which associated rows should be merged

  • assoc_attr (String|Symbol)

    name of the attribute where associated rows will be put



18
19
20
21
# File 'lib/occams-record/merge.rb', line 18

def initialize(target_rows, assoc_attr)
  @target_rows = target_rows
  @assign = "#{assoc_attr}="
end

Instance Attribute Details

#target_rowsArray<OccamsRecord::Results::Row> (readonly)

Returns the rows into which associated rows will be merged.

Returns:



10
11
12
# File 'lib/occams-record/merge.rb', line 10

def target_rows
  @target_rows
end

Instance Method Details

#many!(assoc_rows, target_attr, assoc_attr) ⇒ Object

Merge an array of assoc_rows into the target_rows. Some target_rows may end up with 0 matching associations, and they’ll be assigned empty arrays. target_attr and assoc_attr are the matching keys on target_rows and assoc_rows, respectively.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/occams-record/merge.rb', line 57

def many!(assoc_rows, target_attr, assoc_attr)
  begin
    assoc_rows_by_attr = assoc_rows.group_by(&assoc_attr.to_sym)
  rescue NoMethodError => e
    raise MissingColumnError.new(assoc_rows[0], e.name)
  end

  target_rows.each do |row|
    begin
      pkey = row.send target_attr
    rescue NoMethodError => e
      raise MissingColumnError.new(row, e.name)
    end
    row.send @assign, assoc_rows_by_attr[pkey] || []
  end
end

#single!(assoc_rows, target_attr, assoc_attr) ⇒ Object

Merge a single assoc_row into each target_rows (or nil if one can’t be found). target_attr and assoc_attr are the matching keys on target_rows and assoc_rows, respectively.

Parameters:

  • assoc_rows (Array<OccamsRecord::Results::Row>)

    rows to merge into target_rows

  • target_attr (String|Symbol)

    name of the matching key on the target records

  • assoc_attr (String)

    name of the matching key on the associated records



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/occams-record/merge.rb', line 31

def single!(assoc_rows, target_attr, assoc_attr)
  assoc_rows_by_id = assoc_rows.reduce({}) { |a, assoc_row|
    begin
      id = assoc_row.send assoc_attr
    rescue NoMethodError => e
      raise MissingColumnError.new(assoc_row, e.name)
    end
    a[id] = assoc_row
    a
  }

  target_rows.each do |row|
    begin
      attr = row.send target_attr
    rescue NoMethodError => e
      raise MissingColumnError.new(row, e.name)
    end
    row.send @assign, attr ? assoc_rows_by_id[attr] : nil
  end
end