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,
  ':blank' => '',
  ':empty' => ''
}.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.



45
46
47
48
49
50
51
# File 'lib/restful_query/condition.rb', line 45

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



53
54
55
56
57
# File 'lib/restful_query/condition.rb', line 53

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



84
85
86
87
88
89
90
# File 'lib/restful_query/condition.rb', line 84

def placeholder
  if ['IN', 'NOT IN'].include?(operator)
    '(?)'
  else
    '?'
  end
end

#to_condition_arrayObject



80
81
82
# File 'lib/restful_query/condition.rb', line 80

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

#to_hashObject



76
77
78
# File 'lib/restful_query/condition.rb', line 76

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