Class: Philtre::PredicateSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/philtre/predicate_splitter.rb

Overview

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).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ PredicateSplitter

Returns a new instance of PredicateSplitter.



11
12
13
# File 'lib/philtre/predicate_splitter.rb', line 11

def initialize( key, value )
  @key, @value = key, value
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/philtre/predicate_splitter.rb', line 15

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



15
16
17
# File 'lib/philtre/predicate_splitter.rb', line 15

def value
  @value
end

Instance Method Details

#fieldObject

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.



31
32
33
# File 'lib/philtre/predicate_splitter.rb', line 31

def field
  (@field || @key).andand.to_sym
end

#opObject

the operator, or predicate



36
37
38
# File 'lib/philtre/predicate_splitter.rb', line 36

def op
  @op.andand.to_sym
end

#split_key(suffix) ⇒ Object Also known as: ===, =~

split suffix from the key and store the two values as name and op return truthy if successful



19
20
21
22
23
# File 'lib/philtre/predicate_splitter.rb', line 19

def split_key( suffix )
  rv = @key =~ /\A(?:(.*?)_)?(#{suffix})\z/
  @field, @op = $1, $2
  rv
end