Class: Burner::Library::Collection::Group
- Inherits:
-
JobWithRegister
- Object
- Job
- JobWithRegister
- Burner::Library::Collection::Group
- 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
Instance Attribute Summary collapse
-
#insensitive ⇒ Object
readonly
Returns the value of attribute insensitive.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
Attributes inherited from JobWithRegister
Attributes inherited from Job
Instance Method Summary collapse
-
#initialize(insensitive: false, keys: [], name: '', register: DEFAULT_REGISTER, separator: '') ⇒ Group
constructor
A new instance of Group.
- #perform(output, payload) ⇒ Object
Methods included from Util::Keyable
Methods included from Util::Arrayable
Constructor Details
#initialize(insensitive: false, keys: [], name: '', register: DEFAULT_REGISTER, separator: '') ⇒ Group
Returns a new instance of Group.
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
#insensitive ⇒ Object (readonly)
Returns the value of attribute insensitive.
36 37 38 |
# File 'lib/burner/library/collection/group.rb', line 36 def insensitive @insensitive end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
36 37 38 |
# File 'lib/burner/library/collection/group.rb', line 36 def keys @keys end |
#resolver ⇒ Object (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 |