Sha256: 070d4fc018e42e3b318cfc4450a0a34574cfac14e7366328afa69c1a91730bb3

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module ROM
  module RA
    class Operation

      class Group
        # FIXME: only reading from a relation should be allowed here so this is
        #       obviously too much
        include Charlatan.new(:relation)

        include Enumerable

        attr_reader :options, :header

        def initialize(relation, options)
          super
          @options = options
          @header = relation.header + options.keys - attribute_names
        end

        def each(&block)
          return to_enum unless block

          result = relation.each_with_object({}) do |tuple, grouped|
            left = tuple.reject { |k,_| attribute_names.include?(k) }
            right = tuple.reject { |k,_| !attribute_names.include?(k) }

            grouped[left] ||= {}
            grouped[left][key] ||= []
            grouped[left][key] << right if right.values.any?
          end

          result.map { |k,v| k.merge(v) }.each(&block)
        end

        def key
          options.keys.first
        end

        def attribute_names
          options.values.first
        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rom-0.4.2 lib/rom/ra/operation/group.rb