Sha256: 4cd5dcefe6b0d0175b260d2ad0a37a0e739b4a39279134e010917ffb31b4c7c9

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

module HashPivot
  module Repository
    module Base
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        # @param [Array<Hash>,ActiveRecord::Relation] data
        # @param [Array] group
        # @param [Object] pivot_in
        # @return [Hash]
        def hash_by_group(data, group, pivot_in)
          new(data, group, pivot_in).hash_by_group
        end
      end

      # @param [Array<Hash>] data
      # @param [Array] group
      # @param [Object] pivot_in
      def initialize(data, group, pivot_in)
        @data = translate_data(data)
        @group = group
        @pivot_in = pivot_in
      end

      # @return [Hash]
      def hash_by_group
        @data.group_by do |hash|
          @group.each_with_object({}) do |key, memo_obj|
            memo_obj[key] = hash[key]
          end
        end
      end

      # @param [Array<Hash>] _data
      # @return [Array<Hash>]
      def translate_data(_data)
        raise HashPivot::Error::NotImplementedError, 'translate_data is not implemented'
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hash_pivot-0.3.1 lib/hash_pivot/repository/base.rb
hash_pivot-0.3.0 lib/hash_pivot/repository/base.rb
hash_pivot-0.2.0 lib/hash_pivot/repository/base.rb
hash_pivot-0.1.0 lib/hash_pivot/repository/base.rb