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.18.0 lib/bmg/operator/allbut.rb
bmg-0.17.8 lib/bmg/operator/allbut.rb
bmg-0.17.7 lib/bmg/operator/allbut.rb
bmg-0.17.6 lib/bmg/operator/allbut.rb
bmg-0.17.5 lib/bmg/operator/allbut.rb
bmg-0.17.4 lib/bmg/operator/allbut.rb
bmg-0.17.3 lib/bmg/operator/allbut.rb
bmg-0.17.2 lib/bmg/operator/allbut.rb
bmg-0.16.7 lib/bmg/operator/allbut.rb
bmg-0.17.1 lib/bmg/operator/allbut.rb
bmg-0.17.0 lib/bmg/operator/allbut.rb
bmg-0.16.6 lib/bmg/operator/allbut.rb
bmg-0.16.5 lib/bmg/operator/allbut.rb
bmg-0.16.4 lib/bmg/operator/allbut.rb
bmg-0.16.3 lib/bmg/operator/allbut.rb
bmg-0.16.2 lib/bmg/operator/allbut.rb
bmg-0.16.1 lib/bmg/operator/allbut.rb
bmg-0.16.0 lib/bmg/operator/allbut.rb
bmg-0.16.0.pre.rc2 lib/bmg/operator/allbut.rb
bmg-0.16.0.pre.rc1 lib/bmg/operator/allbut.rb