Sha256: 39b988341dda9e74e958059bd4a445a2389cf28654eecd83c44b678b58897c34

Contents?: true

Size: 992 Bytes

Versions: 3

Compression:

Stored size: 992 Bytes

Contents

module ROM
  module RA
    class Operation

      class Wrap
        # 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

          results = relation.to_a.each_with_object([]) do |tuple, wrapped|
            result = tuple.reject { |k,_| attribute_names.include?(k) }
            result[key] = tuple.reject { |k,_| !attribute_names.include?(k) }

            wrapped << result
          end

          results.each(&block)
        end

        def key
          options.keys.first
        end

        def attribute_names
          options.values.first
        end

      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-0.4.0 lib/rom/ra/operation/wrap.rb
rom-0.3.1 lib/rom/ra/operation/wrap.rb
rom-0.3.0 lib/rom/ra/operation/wrap.rb