Sha256: 6f412025e3531d56612f67517ca98483a9fa29ec5c9d9eac01000e0c8be45206

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require 'hash_op/deep'

# A module to perform group operations on hashes.
module HashOp
  module Grouping

    # @param hashes [Array] hashes to be grouped
    # @param paths [Object] path on which the hashes must be
    #   grouped
    # @return [Hash]
    def group_on_path(hashes, path)
      hashes.inject({}) do |result, hash|
        value_at_path = HashOp::Deep.fetch(hash, path)
        result[value_at_path] ||= []
        result[value_at_path] << hash
        result
      end
    end
    module_function :group_on_path

    # @param hashes [Array] hashes to be grouped
    # @param paths [Array] paths on which the hashes must be
    #   grouped, by order of grouping (1st group-level first)
    # @return [Hash]
    def group_on_paths(hashes, paths)
      return group_on_path(hashes, paths.first) if paths.length == 1

      path = paths.first
      path_groups = group_on_path(hashes, path)
      path_groups.each do |group_key, grouped_hashes|
        path_groups[group_key] = group_on_paths(grouped_hashes, paths[1..-1])
      end
    end
    module_function :group_on_paths
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hash_op-0.3.0 lib/hash_op/grouping.rb
hash_op-0.2.0 lib/hash_op/grouping.rb