Class: Burner::Library::Collection::Group

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

Overview

Take a register’s value (an array of objects) and group the objects by the specified keys. It essentially creates a hash from an array. This is useful for creating a O(1) lookup which can then be used in conjunction with the Coalesce Job for another array of data. It is worth noting that the resulting hashes values are singular objects and not an array like Ruby’s Enumerable#group_by method.

If the insensitive option is set as true then each key’s value will be coerced as a lowercase string. This can help provide two types of insensitivity: case and type insensitivity. This may be appropriate in some places but not others. If any other value coercion is needed then another option would be to first transform the records before grouping them.

An example of this specific job:

input: [{ id: 1, code: ‘a’ }, { id: 2, code: ‘b’ }] keys: [:code] output: { [‘a’] => { id: 1, code: ‘a’ }, [‘b’] => { id: 2, code: ‘b’ } }

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

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(insensitive: false, keys: [], name: '', register: DEFAULT_REGISTER, separator: '') ⇒ Group

Returns a new instance of Group.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/burner/library/collection/group.rb', line 38

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

  @insensitive = insensitive || false
  @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

#insensitiveObject (readonly)

Returns the value of attribute insensitive.



36
37
38
# File 'lib/burner/library/collection/group.rb', line 36

def insensitive
  @insensitive
end

#keysObject (readonly)

Returns the value of attribute keys.



36
37
38
# File 'lib/burner/library/collection/group.rb', line 36

def keys
  @keys
end

#resolverObject (readonly)

Returns the value of attribute resolver.



36
37
38
# File 'lib/burner/library/collection/group.rb', line 36

def resolver
  @resolver
end

Instance Method Details

#perform(output, payload) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/burner/library/collection/group.rb', line 56

def perform(output, payload)
  payload[register] = array(payload[register])
  count             = payload[register].length

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

  grouped_records = payload[register].each_with_object({}) do |record, memo|
    key       = make_key(record, keys, resolver, insensitive)
    memo[key] = record
  end

  payload[register] = grouped_records
end