Class: HashMath::Unpivot::Pivot

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_math/unpivot/pivot.rb

Overview

A single pivot definition consists of which columns to coalesce and to where.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keys:, coalesce_key:, coalesce_key_value:) ⇒ Pivot

keys is an array of keys to include in the un-pivoting. coalesce_key is the new key to use. coalesce_key_value is the new key to use for its corresponding values.



23
24
25
26
27
28
29
# File 'lib/hash_math/unpivot/pivot.rb', line 23

def initialize(keys:, coalesce_key:, coalesce_key_value:)
  @keys               = Array(keys)
  @coalesce_key       = coalesce_key
  @coalesce_key_value = coalesce_key_value

  freeze
end

Instance Attribute Details

#coalesce_keyObject (readonly)

Returns the value of attribute coalesce_key.



16
17
18
# File 'lib/hash_math/unpivot/pivot.rb', line 16

def coalesce_key
  @coalesce_key
end

#coalesce_key_valueObject (readonly)

Returns the value of attribute coalesce_key_value.



16
17
18
# File 'lib/hash_math/unpivot/pivot.rb', line 16

def coalesce_key_value
  @coalesce_key_value
end

#keysObject (readonly)

Returns the value of attribute keys.



16
17
18
# File 'lib/hash_math/unpivot/pivot.rb', line 16

def keys
  @keys
end

Instance Method Details

#expand(base_hash, value_hash) ⇒ Object

The most rudimentary portion of the Unpivoting algorithm, this method works on just one pivot and returns the extrapolated, un-pivoted rows. Takes two hashes as input: the first will serve as the prototype for each returned hash the second will be one to use for value extraction. Returns an array of hashes.



37
38
39
40
41
42
43
44
# File 'lib/hash_math/unpivot/pivot.rb', line 37

def expand(base_hash, value_hash) # :nodoc:
  keys.map do |key|
    base_hash.merge(
      coalesce_key => key,
      coalesce_key_value => value_hash[key]
    )
  end
end