lib/hash_pivot/table.rb in hash_pivot-0.3.0 vs lib/hash_pivot/table.rb in hash_pivot-0.3.1
- old
+ new
@@ -4,65 +4,53 @@
require_relative './repository/active_record_repository'
require_relative './repository/struct_repository'
module HashPivot
class Table
+ # rubocop:disable Layout/LineLength
+
# @param [Array<Hash>] data
+ # @param [Class<HashPivot::Repository::HashRepository,HashPivot::Repository::StructRepository,HashPivot::Repository::ActiveRecordRepository>] repository
def initialize(data, repository: HashPivot::Repository::HashRepository)
@data = data
@repository = repository
end
+ # rubocop:enable Layout/LineLength
+
# @param [Array, Object] group
# @param [Object] pivot_in
- # @param [Array] pivot_kinds
+ # @param [Hash,nil] pivot_kinds
+ # @param [Proc] block
+ # @return [Array<Hash>]
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 = pivot_with_sum(pivot_kinds, array, pivot_in, &block)
+ hash = transpose_with(pivot_kinds, array, pivot_in, &block)
memo << key.merge(hash)
end
end
private
- # @param [Array,Hash] pivot_kinds
+ # @param [Hash,nil] pivot_kinds
# @param [Array] array
# @param [Object] pivot_in
# @param [Proc] block
# @return [Hash]
- def pivot_with_sum(pivot_kinds, array, pivot_in, &block)
- pivot_kinds ||= array.map { |h| h[pivot_in] }.uniq.compact
- case pivot_kinds
- when Array
- pivot_with_sum_with_array_pivot_kinds(pivot_kinds, array, pivot_in, &block)
- when Hash
- pivot_with_sum_with_hash_pivot_kinds(pivot_kinds, array, pivot_in, &block)
- end
- end
-
- # @param [Array,Hash] pivot_kinds
- # @param [Array] array
- # @param [Object] pivot_in
- # @param [Proc] block
- # @return [Hash]
- def pivot_with_sum_with_array_pivot_kinds(pivot_kinds, array, pivot_in, &block)
- pivot_kinds.each_with_object({}) do |pivot_kind, memo|
+ def transpose_with(pivot_kinds, array, pivot_in, &block)
+ pivot_kinds ||= calculated_pivot_kinds_from(array, pivot_in)
+ pivot_kinds.each_with_object({}) do |(pivot_kind, pivot_label), memo|
pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
- memo[pivot_kind] = block ? yield(pivoted_data) : pivoted_data
+ memo[pivot_label] = block ? yield(pivoted_data) : pivoted_data
end
end
- # @param [Array,Hash] pivot_kinds
# @param [Array] array
# @param [Object] pivot_in
- # @param [Proc] block
# @return [Hash]
- def pivot_with_sum_with_hash_pivot_kinds(pivot_kinds, array, pivot_in, &block)
- pivot_kinds.each_with_object({}) do |(pivot_kind, pivot_label), memo|
- pivoted_data = array.select { |h| h[pivot_in] == pivot_kind }
- memo[pivot_label] = block ? yield(pivoted_data) : pivoted_data
- end
+ def calculated_pivot_kinds_from(array, pivot_in)
+ array.map { |h| h[pivot_in] }.uniq.compact.each_with_object({}) { |kind, memo| memo[kind] = kind }
end
end
end