Sha256: 539e7847dc37072e9bd017a31930bfff72294cd1299d4c5872f594cc6eb33573

Contents?: true

Size: 1.43 KB

Versions: 31

Compression:

Stored size: 1.43 KB

Contents

module Bmg
  module Operator
    #
    # Rxmatch operator.
    #
    # Filters operand's tuples to those whose attributes match a
    # given string or regular expression.
    #
    class Rxmatch
      include Operator::Unary

      DEFAULT_OPTIONS = {
        case_sensitive: false
      }

      def initialize(type, operand, attrs, matcher, options)
        @type = type
        @operand = operand
        @attrs = attrs
        @matcher = matcher
        @options = DEFAULT_OPTIONS.merge(options)
      end

    protected

      attr_reader :attrs, :matcher, :options

      def case_sensitive?
        !!options[:case_sensitive]
      end

    public

      def each
        return to_enum unless block_given?
        @operand.each do |tuple|
          against = attrs.map{|a| tuple[a] }.join(" ")
          matcher = self.matcher
          unless case_sensitive?
            against = against.downcase
            matcher = matcher.downcase if matcher.is_a?(String)
          end
          yield(tuple) if against.match(matcher)
        end
      end

      def to_ast
        [ :rxmatch, operand.to_ast, attrs.dup, matcher, options.dup ]
      end

    protected

      def _restrict(type, predicate)
        @operand
          .restrict(predicate)
          .rxmatch(attrs, matcher, options)
      end

    protected ### inspect

      def args
        [ attrs, matcher, options ]
      end

    end # class Rxmatch
  end # module Operator
end # module Bmg

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
bmg-0.23.3 lib/bmg/operator/rxmatch.rb
bmg-0.23.2 lib/bmg/operator/rxmatch.rb
bmg-0.23.1 lib/bmg/operator/rxmatch.rb
bmg-0.23.0 lib/bmg/operator/rxmatch.rb
bmg-0.21.5 lib/bmg/operator/rxmatch.rb
bmg-0.21.4 lib/bmg/operator/rxmatch.rb
bmg-0.20.5 lib/bmg/operator/rxmatch.rb
bmg-0.19.3 lib/bmg/operator/rxmatch.rb
bmg-0.21.3 lib/bmg/operator/rxmatch.rb
bmg-0.21.2 lib/bmg/operator/rxmatch.rb
bmg-0.21.0 lib/bmg/operator/rxmatch.rb
bmg-0.20.4 lib/bmg/operator/rxmatch.rb
bmg-0.20.2 lib/bmg/operator/rxmatch.rb
bmg-0.20.1 lib/bmg/operator/rxmatch.rb
bmg-0.20.0 lib/bmg/operator/rxmatch.rb
bmg-0.19.2 lib/bmg/operator/rxmatch.rb
bmg-0.19.1 lib/bmg/operator/rxmatch.rb
bmg-0.19.0 lib/bmg/operator/rxmatch.rb
bmg-0.18.15 lib/bmg/operator/rxmatch.rb
bmg-0.18.14 lib/bmg/operator/rxmatch.rb