lib/restful_query/condition.rb in restful_query-0.2.0 vs lib/restful_query/condition.rb in restful_query-0.3.0

- old
+ new

@@ -3,37 +3,55 @@ class Condition attr_reader :column, :value, :operator, :options OPERATOR_MAPPING = { - 'lt' => '<', - 'gt' => '>', - 'gteq' => '>=', - 'lteq' => '<=', - 'eq' => '=', - 'like' => 'LIKE' + 'lt' => '<', + 'gt' => '>', + 'gteq' => '>=', + 'lteq' => '<=', + 'eq' => '=', + 'neq' => '!=', + 'is' => 'IS', + 'not' => 'IS NOT', + 'like' => 'LIKE', + 'in' => 'IN', + 'notin' => 'NOT IN' }.freeze - - REVERSE_OPERATOR_MAPPING = { - '<' => 'lt', - '>' => 'gt', - '>=' => 'gteq', - '<=' => 'lteq', - '=' => 'eq', - 'LIKE' => 'like' + + CONVERTABLE_VALUES = { + ':true' => true, + ':false' => false, + ':nil' => nil, + ':null' => nil }.freeze + ENGLISH_OPERATOR_MAPPING = { + 'Less than' => 'lt', + 'Greater than' => 'gt', + 'Less than or equal to' => 'lteq', + 'Greater than or equal to' => 'gteq', + 'Equal to' => 'eq', + 'Not equal to' => 'neq', + 'Is' => 'is', + 'Is not' => 'not', + 'Like' => 'like', + 'In' => 'in', + 'Not in' => 'notin' + }.freeze + + def initialize(column, value, operator = '=', options = {}) @options = {} - @options = options if options.is_a?(Hash) + self.options = options if options.is_a?(Hash) self.column = column - self.value = value self.operator = operator + self.value = value end def map_operator(operator_to_look_up, reverse = false) - mapping = reverse ? REVERSE_OPERATOR_MAPPING.dup : OPERATOR_MAPPING.dup + mapping = reverse ? OPERATOR_MAPPING.dup.invert : OPERATOR_MAPPING.dup return operator_to_look_up if mapping.values.include?(operator_to_look_up) found = mapping[operator_to_look_up.to_s] end def operator=(operator) @@ -47,29 +65,42 @@ def value=(value) @value = parse_value(value) end + def options=(options) + options.each {|k, v| @options[k.to_sym] = v } + end + def to_hash {column => {map_operator(operator, true) => value}} end def to_condition_array - parsed_value = if operator == 'LIKE' - "%#{value}%" - elsif options[:integer] - value.to_i - else - value - end - ["#{column} #{operator} ?", parsed_value] + ["#{column} #{operator} #{placeholder}", value] end + def placeholder + if ['IN', 'NOT IN'].include?(operator) + '(?)' + else + '?' + end + end + protected def parse_value(value) - if options[:chronic] + if operator == 'LIKE' + "%#{value}%" + elsif ['IN', 'NOT IN'].include?(operator) && !value.is_a?(Array) + value.split(options[:delimiter] || ',') + elsif options[:integer] + value.to_i + elsif options[:chronic] Chronic.parse(value.to_s) - else + elsif value =~ /^\:/ && CONVERTABLE_VALUES.has_key?(value) + CONVERTABLE_VALUES[value] + else value end end end \ No newline at end of file