Sha256: 5e1403c75ec7c69b54258aa3727053e2ac0f5765082a3bd97acdbd8dcd79f7b3

Contents?: true

Size: 1.84 KB

Versions: 40

Compression:

Stored size: 1.84 KB

Contents

module Bmg
  module Operator
    #
    # Allbut operator.
    #
    # Projects operand's tuples on all but given attributes, that is,
    # removes attributes in the list. The operator takes care of removing
    # duplicates.
    #
    # Example:
    #
    #   [{ a: 1, b: 2 }] allbut [:b] => [{ a: 1 }]
    #
    # All attributes in the butlist SHOULD be existing attributes of the
    # input tuples.
    #
    class Allbut
      include Operator::Unary

      def initialize(type, operand, butlist)
        @type = type
        @operand = operand
        @butlist = butlist
      end

    protected

      attr_reader :butlist

    public

      def each
        seen = {}
        @operand.each do |tuple|
          allbuted = tuple_allbut(tuple)
          unless seen.has_key?(allbuted)
            yield(allbuted)
            seen[allbuted] = true
          end
        end
      end

      def insert(arg)
        case arg
        when Hash       then operand.insert(valid_tuple!(arg))
        when Enumerable then operand.insert(arg.map{|t| valid_tuple!(t) })
        else
          super
        end
      end

      def update(tuple)
        operand.update(valid_tuple!(tuple))
      end

      def delete
        operand.delete
      end

      def to_ast
        [:allbut, operand.to_ast, butlist.dup]
      end

    protected ### optimization

      def _restrict(type, predicate)
        operand.restrict(predicate).allbut(butlist)
      end

    protected ### inspect

      def args
        [ butlist ]
      end

    private

      def tuple_allbut(tuple)
        TupleAlgebra.allbut(tuple, @butlist)
      end

      def valid_tuple!(tuple)
        offending = tuple.keys & butlist
        raise InvalidUpdateError, "#{offending.inspect} cannot be updated" unless offending.empty?
        tuple
      end

    end # class Allbut
  end # module Operator
end # module Bmg

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
bmg-0.15.0 lib/bmg/operator/allbut.rb
bmg-0.14.6 lib/bmg/operator/allbut.rb
bmg-0.14.5 lib/bmg/operator/allbut.rb
bmg-0.14.4 lib/bmg/operator/allbut.rb
bmg-0.14.3 lib/bmg/operator/allbut.rb
bmg-0.14.2 lib/bmg/operator/allbut.rb
bmg-0.14.1 lib/bmg/operator/allbut.rb
bmg-0.14.0 lib/bmg/operator/allbut.rb
bmg-0.13.0 lib/bmg/operator/allbut.rb
bmg-0.12.0 lib/bmg/operator/allbut.rb
bmg-0.11.0 lib/bmg/operator/allbut.rb
bmg-0.10.1 lib/bmg/operator/allbut.rb
bmg-0.10.0 lib/bmg/operator/allbut.rb
bmg-0.9.1 lib/bmg/operator/allbut.rb
bmg-0.9.0 lib/bmg/operator/allbut.rb
bmg-0.8.0 lib/bmg/operator/allbut.rb
bmg-0.7.1 lib/bmg/operator/allbut.rb
bmg-0.7.0 lib/bmg/operator/allbut.rb
bmg-0.6.1 lib/bmg/operator/allbut.rb
bmg-0.6.0 lib/bmg/operator/allbut.rb