Class: Burner::Library::Collection::Unpivot

Inherits:
JobWithRegister show all
Defined in:
lib/burner/library/collection/unpivot.rb

Overview

Take an array of objects and un-pivot groups of keys into rows. Under the hood it uses HashMath’s Unpivot class: github.com/bluemarblepayroll/hash_math

Expected Payload input: array of objects. Payload output: An 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::Arrayable

#array

Constructor Details

#initialize(name: '', pivot_set: HashMath::Unpivot::PivotSet.new, register: DEFAULT_REGISTER) ⇒ Unpivot

Returns a new instance of Unpivot.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/burner/library/collection/unpivot.rb', line 22

def initialize(
  name: '',
  pivot_set: HashMath::Unpivot::PivotSet.new,
  register: DEFAULT_REGISTER
)
  super(name: name, register: register)

  @unpivot = HashMath::Unpivot.new(pivot_set)

  freeze
end

Instance Attribute Details

#unpivotObject (readonly)

Returns the value of attribute unpivot.



20
21
22
# File 'lib/burner/library/collection/unpivot.rb', line 20

def unpivot
  @unpivot
end

Instance Method Details

#perform(output, payload) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/burner/library/collection/unpivot.rb', line 34

def perform(output, payload)
  payload[register] = array(payload[register])
  object_count      = payload[register].length || 0

  message = "#{pivot_count} Pivots, Key(s): #{key_count} key(s), #{object_count} objects(s)"

  output.detail(message)

  payload[register] = payload[register].flat_map { |object| unpivot.expand(object) }
end