Class: HashPivot::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_pivot/table.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, repository: HashPivot::Repository::HashRepository) ⇒ Table

Returns a new instance of Table.

Parameters:



13
14
15
16
# File 'lib/hash_pivot/table.rb', line 13

def initialize(data, repository: HashPivot::Repository::HashRepository)
  @data = data
  @repository = repository
end

Instance Method Details

#pivot(group, pivot_in, pivot_kinds, &block) ⇒ Array<Hash>

Parameters:

  • group (Array, Object)
  • pivot_in (Object)
  • pivot_kinds (Hash, nil)
  • block (Proc)

Returns:

  • (Array<Hash>)


25
26
27
28
29
30
31
32
# File 'lib/hash_pivot/table.rb', line 25

def pivot(group, pivot_in, pivot_kinds, &block)
  group = [group] unless group.is_a?(Array)
  @repository.hash_by_group(@data, group, pivot_in).each_with_object([]) do |(key, array), memo|
    hash = transpose_with(pivot_kinds, array, pivot_in, &block)

    memo << key.merge(hash)
  end
end