Class: Burner::Library::Collection::Coalesce

Inherits:
JobWithRegister show all
Includes:
Util::Keyable
Defined in:
lib/burner/library/collection/coalesce.rb

Overview

This is generally used right after the Group job has been executed on a separate dataset in a separate register. This job can match up specified values in its dataset with lookup values in another. If it finds a match then it will (shallow) copy over the values into the respective dataset.

Expected Payload input: array of objects. Payload output: array of objects.

Constant Summary

Constants inherited from JobWithRegister

JobWithRegister::BLANK

Instance Attribute Summary collapse

Attributes inherited from JobWithRegister

#register

Attributes inherited from Job

#name

Instance Method Summary collapse

Methods included from Util::Keyable

#make_key

Methods included from Util::Arrayable

#array

Constructor Details

#initialize(grouped_register:, insensitive: false, key_mappings: [], keys: [], name: '', register: DEFAULT_REGISTER, separator: '') ⇒ Coalesce

Returns a new instance of Coalesce.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/burner/library/collection/coalesce.rb', line 29

def initialize(
  grouped_register:,
  insensitive: false,
  key_mappings: [],
  keys: [],
  name: '',
  register: DEFAULT_REGISTER,
  separator: ''
)
  super(name: name, register: register)

  @grouped_register = grouped_register.to_s
  @insensitive      = insensitive || false
  @key_mappings     = Modeling::KeyMapping.array(key_mappings)
  @keys             = Array(keys)
  @resolver         = Objectable.resolver(separator: separator.to_s)

  raise ArgumentError, 'at least one key is required' if @keys.empty?

  freeze
end

Instance Attribute Details

#grouped_registerObject (readonly)

Returns the value of attribute grouped_register.



23
24
25
# File 'lib/burner/library/collection/coalesce.rb', line 23

def grouped_register
  @grouped_register
end

#insensitiveObject (readonly)

Returns the value of attribute insensitive.



23
24
25
# File 'lib/burner/library/collection/coalesce.rb', line 23

def insensitive
  @insensitive
end

#key_mappingsObject (readonly)

Returns the value of attribute key_mappings.



23
24
25
# File 'lib/burner/library/collection/coalesce.rb', line 23

def key_mappings
  @key_mappings
end

#keysObject (readonly)

Returns the value of attribute keys.



23
24
25
# File 'lib/burner/library/collection/coalesce.rb', line 23

def keys
  @keys
end

#resolverObject (readonly)

Returns the value of attribute resolver.



23
24
25
# File 'lib/burner/library/collection/coalesce.rb', line 23

def resolver
  @resolver
end

Instance Method Details

#perform(output, payload) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/burner/library/collection/coalesce.rb', line 51

def perform(output, payload)
  ensure_array(payload)

  count = payload[register].length

  output.detail("Coalescing based on key(s): #{keys} for #{count} records(s)")

  payload[register].each do |record|
    key    = make_key(record, keys, resolver, insensitive)
    lookup = find_lookup(payload, key)

    key_mappings.each do |key_mapping|
      value = resolver.get(lookup, key_mapping.from)

      resolver.set(record, key_mapping.to, value)
    end
  end
end