Sha256: 034169dc96785cf89bc762bec30070b816e4c8fdea10686f1c25073711f381e6

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

module Alf
  module Engine
    #
    # Filters operand using a tuple predicate.
    #
    # Example
    #
    #     rel = [
    #       {:name => "Smith"},
    #       {:name => "Jones"}
    #     ]
    #     Filter.new(rel, Predicate.parse("name =~ /^J/")).to_a
    #     # => [
    #     #      {:name => "Jones"}
    #     #    ]
    #
    class Filter
      include Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [Predicate] Filtering predicate
      attr_reader :predicate

      # Creates a Filter instance
      def initialize(operand, predicate, expr = nil, compiler = nil)
        super(expr, compiler)
        @operand = operand
        @predicate = predicate
      end

      # (see Cog#each)
      def _each
        scope = tuple_scope
        operand.each do |tuple|
          t = scope.__set_tuple(tuple)
          yield(tuple) if @predicate.evaluate(t)
        end
      end

      def arguments
        [ predicate ]
      end

    end # class Filter
  end # module Engine
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/engine/filter.rb
alf-core-0.16.2 lib/alf/engine/filter.rb
alf-core-0.16.1 lib/alf/engine/filter.rb
alf-core-0.16.0 lib/alf/engine/filter.rb
alf-core-0.15.0 lib/alf/engine/filter.rb