Class: RestfulQuery::Condition

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = {})
  @options = {}
  self.options  = options if options.is_a?(Hash)
  self.column   = column
  self.operator = operator
  self.value    = value
end

Instance Attribute Details

#columnObject

Returns the value of attribute column.



5
6
7
# File 'lib/restful_query/condition.rb', line 5

def column
  @column
end

#operatorObject

Returns the value of attribute operator.



5
6
7
# File 'lib/restful_query/condition.rb', line 5

def operator
  @operator
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/restful_query/condition.rb', line 5

def options
  @options
end

#valueObject

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

#placeholderObject



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_arrayObject



78
79
80
# File 'lib/restful_query/condition.rb', line 78

def to_condition_array
  ["#{column} #{operator} #{placeholder}", value]
end

#to_hashObject



74
75
76
# File 'lib/restful_query/condition.rb', line 74

def to_hash
  {column => {map_operator(operator, true) => value}}
end