Class: RestfulQuery::Condition
- Inherits:
-
Object
- Object
- RestfulQuery::Condition
- Defined in:
- lib/restful_query/condition.rb
Constant Summary collapse
- OPERATOR_MAPPING =
{ 'lt' => '<', 'gt' => '>', 'gteq' => '>=', 'lteq' => '<=', 'eq' => '=', 'neq' => '!=', 'is' => 'IS', 'not' => 'IS NOT', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN' }.freeze
- 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
Instance Attribute Summary collapse
-
#column ⇒ Object
Returns the value of attribute column.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#options ⇒ Object
Returns the value of attribute options.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(column, value, operator = '=', options = {}) ⇒ Condition
constructor
A new instance of Condition.
- #map_operator(operator_to_look_up, reverse = false) ⇒ Object
- #placeholder ⇒ Object
- #to_condition_array ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(column, value, operator = '=', options = {}) ⇒ Condition
Returns a new instance of Condition.
43 44 45 46 47 48 49 |
# File 'lib/restful_query/condition.rb', line 43 def initialize(column, value, operator = '=', = {}) @options = {} self. = if .is_a?(Hash) self.column = column self.operator = operator self.value = value end |
Instance Attribute Details
#column ⇒ Object
Returns the value of attribute column.
5 6 7 |
# File 'lib/restful_query/condition.rb', line 5 def column @column end |
#operator ⇒ Object
Returns the value of attribute operator.
5 6 7 |
# File 'lib/restful_query/condition.rb', line 5 def operator @operator end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/restful_query/condition.rb', line 5 def @options end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/restful_query/condition.rb', line 5 def value @value end |
Instance Method Details
#map_operator(operator_to_look_up, reverse = false) ⇒ Object
51 52 53 54 55 |
# File 'lib/restful_query/condition.rb', line 51 def map_operator(operator_to_look_up, reverse = false) 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 |
#placeholder ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/restful_query/condition.rb', line 82 def placeholder if ['IN', 'NOT IN'].include?(operator) '(?)' else '?' end end |
#to_condition_array ⇒ Object
78 79 80 |
# File 'lib/restful_query/condition.rb', line 78 def to_condition_array ["#{column} #{operator} #{placeholder}", value] end |
#to_hash ⇒ Object
74 75 76 |
# File 'lib/restful_query/condition.rb', line 74 def to_hash {column => {map_operator(operator, true) => value}} end |