Sha256: 63fc1d1eb78135305d9efd9e0767ce2b1e0a37d0d334320e92d37b1bda212fb1

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

require 'fastandand'

module Philtre
  # This is to split things like birth_year_gt into
  #  - birth_year (the field)
  #  - gt (the predicate)
  # Yes, there are side effects.
  # === is provided so it can be used in case statements
  # (which doesn't really work cos they're backwards).
  class PredicateSplitter
    def initialize( key, value )
      @key, @value = key, value
    end

    attr_reader :key, :value

    # split suffix from the key and store the two values as name and op
    # return truthy if successful
    def split_key( suffix )
      rv = @key =~ /(.*?)_?(#{suffix})$/
      @field, @op = $1, $2
      rv
    end

    alias_method :===, :split_key
    alias_method :=~, :split_key

    # return name if the split was successful, or fall back to key
    # which is handy when none of the predicates match and so key
    # is probably just a field name.
    def field
      (@field || @key).andand.to_sym
    end

    # the operator, or predicate
    def op
      @op.andand.to_sym
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
philtre-0.1.0 lib/philtre/predicate_splitter.rb
philtre-0.0.1 lib/philtre/predicate_splitter.rb