Class: CloneKit::MergeAttributesTool

Inherits:
Object
  • Object
show all
Defined in:
lib/clone_kit/merge_attributes_tool.rb

Overview

Given an array of hashes representing records, this class is able to resolve values among them using a variety of strategies. The strategies all merge right-to-left, meaning that the last record is given precidence over the first.

hashes

assigns to a target hash a list of hash attributes that are (deeply)
from the others

arrays

assigns to a target hash a list of array attributes that are concatenated
and uniquified from the others

cluster

assigns to a target hash a list of attributes that are copied from the first
record that returns true using the given block

last

assigns to a target hash a list of attributes that are copied from the
last record

any

assigns to a target hash a list of attributes from any other record where
that attribute is not blank.

max/min

assigns to a target hash the maximum/minimum value from other records for each
from a list of attribute

Instance Method Summary collapse

Constructor Details

#initialize(mergeable) ⇒ MergeAttributesTool

Returns a new instance of MergeAttributesTool.



33
34
35
# File 'lib/clone_kit/merge_attributes_tool.rb', line 33

def initialize(mergeable)
  self.mergeable = mergeable
end

Instance Method Details

#any(target, *attributes) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/clone_kit/merge_attributes_tool.rb', line 71

def any(target, *attributes)
  attributes.each do |att|
    mergeable.reverse_each do |m|
      val = m[att]
      if val.present?
        target[att] = val
        break
      end
    end
  end
end

#arrays(target, *attributes) ⇒ Object



47
48
49
50
51
52
# File 'lib/clone_kit/merge_attributes_tool.rb', line 47

def arrays(target, *attributes)
  attributes.each do |att|
    new_val = mergeable.flat_map { |m| m[att] }.uniq
    target[att] = new_val
  end
end

#cluster(target, *attributes) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/clone_kit/merge_attributes_tool.rb', line 54

def cluster(target, *attributes)
  mergeable.reverse_each do |m|
    next unless yield m

    attributes.each do |att|
      target[att] = m[att]
    end
    break
  end
end

#hashes(target, *attributes) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/clone_kit/merge_attributes_tool.rb', line 37

def hashes(target, *attributes)
  attributes.each do |att|
    result = {}
    mergeable.each do |m|
      result = result.deep_merge(m[att])
    end
    target[att] = result
  end
end

#last(target, *attributes) ⇒ Object



65
66
67
68
69
# File 'lib/clone_kit/merge_attributes_tool.rb', line 65

def last(target, *attributes)
  attributes.each do |att|
    target[att] = mergeable[-1][att]
  end
end

#max(target, *attributes) ⇒ Object



83
84
85
86
87
# File 'lib/clone_kit/merge_attributes_tool.rb', line 83

def max(target, *attributes)
  attributes.each do |att|
    target[att] = mergeable.map { |m| m[att] }.compact.max
  end
end

#min(target, *attributes) ⇒ Object



89
90
91
92
93
# File 'lib/clone_kit/merge_attributes_tool.rb', line 89

def min(target, *attributes)
  attributes.each do |att|
    target[att] = mergeable.map { |m| m[att] }.compact.min
  end
end