Class: Burner::Library::Collection::Pivot
- Inherits:
-
JobWithRegister
- Object
- Job
- JobWithRegister
- Burner::Library::Collection::Pivot
- Defined in:
- lib/burner/library/collection/pivot.rb
Overview
Take an array of objects and pivot a key into multiple keys. It essentially takes all the values for a key and creates N number of keys (one per value.) Under the hood it uses HashMath’s Record and Table classes: github.com/bluemarblepayroll/hash_math
An example of a normalized dataset that could be pivoted:
records = [
{ patient_id: 1, key: :first_name, value: 'bozo' },
{ patient_id: 1, key: :last_name, value: 'clown' },
{ patient_id: 2, key: :first_name, value: 'frank' },
{ patient_id: 2, key: :last_name, value: 'rizzo' },
]
Using the following job configuration:
config =
unique_key: :patient_id
Once ran through this job, it would set the register to:
records = [
{ patient_id: 1, first_name: 'bozo', last_name: 'clown' },
{ patient_id: 2, first_name: 'frank', last_name: 'rizzo' },
]
Expected Payload input: array of objects. Payload output: An array of objects.
Constant Summary collapse
- DEFAULT_PIVOT_KEY =
:key
- DEFAULT_PIVOT_VALUE_KEY =
:value
Constants inherited from JobWithRegister
Instance Attribute Summary collapse
-
#insensitive ⇒ Object
readonly
Returns the value of attribute insensitive.
-
#non_pivoted_keys ⇒ Object
readonly
Returns the value of attribute non_pivoted_keys.
-
#other_keys ⇒ Object
readonly
Returns the value of attribute other_keys.
-
#pivot_key ⇒ Object
readonly
Returns the value of attribute pivot_key.
-
#pivot_value_key ⇒ Object
readonly
Returns the value of attribute pivot_value_key.
-
#resolver ⇒ Object
readonly
Returns the value of attribute resolver.
-
#unique_keys ⇒ Object
readonly
Returns the value of attribute unique_keys.
Attributes inherited from JobWithRegister
Attributes inherited from Job
Instance Method Summary collapse
-
#initialize(unique_keys:, insensitive: false, name: '', other_keys: [], pivot_key: DEFAULT_PIVOT_KEY, pivot_value_key: DEFAULT_PIVOT_KEY_VALUE, register: DEFAULT_REGISTER, separator: '') ⇒ Pivot
constructor
A new instance of Pivot.
- #perform(output, payload) ⇒ Object
Methods included from Util::Arrayable
Constructor Details
#initialize(unique_keys:, insensitive: false, name: '', other_keys: [], pivot_key: DEFAULT_PIVOT_KEY, pivot_value_key: DEFAULT_PIVOT_KEY_VALUE, register: DEFAULT_REGISTER, separator: '') ⇒ Pivot
Returns a new instance of Pivot.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/burner/library/collection/pivot.rb', line 54 def initialize( unique_keys:, insensitive: false, name: '', other_keys: [], pivot_key: DEFAULT_PIVOT_KEY, pivot_value_key: DEFAULT_PIVOT_KEY_VALUE, register: DEFAULT_REGISTER, separator: '' ) super(name: name, register: register) @insensitive = insensitive || false @pivot_key = pivot_key.to_s @pivot_value_key = pivot_value_key.to_s @resolver = Objectable.resolver(separator: separator) @unique_keys = Array(unique_keys) @other_keys = Array(other_keys) @non_pivoted_keys = @unique_keys + @other_keys freeze end |
Instance Attribute Details
#insensitive ⇒ Object (readonly)
Returns the value of attribute insensitive.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def insensitive @insensitive end |
#non_pivoted_keys ⇒ Object (readonly)
Returns the value of attribute non_pivoted_keys.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def non_pivoted_keys @non_pivoted_keys end |
#other_keys ⇒ Object (readonly)
Returns the value of attribute other_keys.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def other_keys @other_keys end |
#pivot_key ⇒ Object (readonly)
Returns the value of attribute pivot_key.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def pivot_key @pivot_key end |
#pivot_value_key ⇒ Object (readonly)
Returns the value of attribute pivot_value_key.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def pivot_value_key @pivot_value_key end |
#resolver ⇒ Object (readonly)
Returns the value of attribute resolver.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def resolver @resolver end |
#unique_keys ⇒ Object (readonly)
Returns the value of attribute unique_keys.
46 47 48 |
# File 'lib/burner/library/collection/pivot.rb', line 46 def unique_keys @unique_keys end |
Instance Method Details
#perform(output, payload) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/burner/library/collection/pivot.rb', line 77 def perform(output, payload) objects = array(payload[register]) table = make_table(objects) output.detail("Pivoting #{objects.length} object(s)") output.detail("By key: #{pivot_key} and value: #{pivot_value_key}") objects.each { |object| object_to_table(object, table) } pivoted_objects = table.to_a.map(&:fields) output.detail("Resulting dataset has #{pivoted_objects.length} object(s)") payload[register] = pivoted_objects end |