class Yap::FilterValue

Filter values can have the following formats:

* Integer - e.g. 100
* String - e.g. text
* Comparisons - e.g. >10, <=B
* Dates - e.g. 1970-01-01 (depends on the database backend)
* Ranges - e.g. 1...2, Jones..Smith
* NULL -  e.g. NULL, null, Null
* Negation - e.g. !value, !1..10

Constants

OPERATOR_INVERSION_MAP

Attributes

condition[R]
value[R]

Public Instance Methods

parse_value(value) click to toggle source
# File lib/yap/filter_value.rb, line 20
def parse_value(value)
  value = handle_negation(value)

  @value = case value
  when /([<>]=?)(.+)/
    handle_comparison_operators($1.to_sym, $2)
  when /(.+)(\.{2,3})(.+)/
    Range.new $1, $3, $2 == '...'
  else
    handle_null(value)
  end
end

Private Instance Methods

handle_comparison_operators(operator, value) click to toggle source
# File lib/yap/filter_value.rb, line 46
def handle_comparison_operators(operator, value)
  case operator
  when :<, :> then invert_comparison_operator(operator, value)
  when :<= then ExtendedRange.new(-String::INFINITY, value)
  when :>= then ExtendedRange.new(value, String::INFINITY)
  end
end
handle_negation(value) click to toggle source
# File lib/yap/filter_value.rb, line 35
def handle_negation(value)
  if value =~ /^!(.+)$/
    @condition = :not
    value = $1
  else
    @condition = :where
  end

  value
end
handle_null(value) click to toggle source
# File lib/yap/filter_value.rb, line 63
def handle_null(value)
  value.casecmp('null').zero? ? nil : value
end
invert_comparison_operator(operator, value) click to toggle source
# File lib/yap/filter_value.rb, line 54
def invert_comparison_operator(operator, value)
  toggle_condition!
  handle_comparison_operators(OPERATOR_INVERSION_MAP[operator], value)
end
toggle_condition!() click to toggle source
# File lib/yap/filter_value.rb, line 59
def toggle_condition!
  @condition = @condition == :where ? :not : :where
end