Class: RQuery::OperationCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/rquery/operation_collector.rb

Constant Summary collapse

NOT_PREFIX =
'not_'
NIL_PREFIX =
''

Instance Method Summary collapse

Constructor Details

#initializeOperationCollector

Returns a new instance of OperationCollector.



6
7
8
9
10
# File 'lib/rquery/operation_collector.rb', line 6

def initialize
  # @name, @prefix = optns[:name], optns[:prefix]
  @operations = []
  @values = []
end

Instance Method Details

#&(second) ⇒ Object

used to group operations for anding on a single line example with sqlite adapter (user.age.in [1,2,3]) | (user.name.contains “foo”)

>(age in (?) and name like ‘%’ || ‘foo’ || ‘%’)



69
70
71
# File 'lib/rquery/operation_collector.rb', line 69

def &(second)
  group :and
end

#add_operation(val) ⇒ Object

add and operation to the @operations array which will be popped and pushed depending on the operations sequence and arrangement



23
24
25
26
# File 'lib/rquery/operation_collector.rb', line 23

def add_operation(val)
  @operations << val.to_s
  self
end

#between(*args) ⇒ Object Also known as: from



85
86
87
# File 'lib/rquery/operation_collector.rb', line 85

def between(*args)
  call_between(NIL_PREFIX, *args)
end

#contains(str) ⇒ Object



93
94
95
96
# File 'lib/rquery/operation_collector.rb', line 93

def contains(str)
  @values << str
  chain :contains
end

#group(type) ⇒ Object

grouping is done by using the | and & operators between multiple operations objects on a single line.

This works because each operation ie (user.age.is == 2) is evaluated before these two operators thus pushing the equivelant operation string onto the @operations array (ie ‘age = ?’). When an operation is evaluated it returns the Operations class which can be compared using the aforementioned operators. Those operators call the group method popping the last two arguments off the stack and dealing with them in one of two ways

  1. if the second object popped is a string both objects should be

added to a new OperationGroup which is then put back onto the stack
  1. if the second object popped is an OperationGroup the firest belongs to this group as

well (it was on the same line). It is added to the OperationGroup and put back on the
stack

TODO requires refactoring



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rquery/operation_collector.rb', line 46

def group(type)
  second_op, first_op = @operations.pop, @operations.pop
  
  #if the previous operation on the stack is an Operation Group we need to add to it
  #and push it back on the @operations stack
  if first_op.class == OperationsGroup
    if first_op.type == type
      first_op.ops << second_op
      @operations << first_op
    else
      @operations << OperationsGroup.new(first_op.to_s, second_op, type)
    end
  else
    @operations << OperationsGroup.new(first_op, second_op, type)
  end

  self
end

#in(*args) ⇒ Object



77
78
79
# File 'lib/rquery/operation_collector.rb', line 77

def in(*args)
  call_in(NIL_PREFIX, *args)
end

#not=(arg) ⇒ Object



103
104
105
106
# File 'lib/rquery/operation_collector.rb', line 103

def not=(arg)
  @values << arg
  chain :neq
end

#not_between(*args) ⇒ Object Also known as: not_from



89
90
91
# File 'lib/rquery/operation_collector.rb', line 89

def not_between(*args)
  call_between(NOT_PREFIX, *args)
end

#not_in(*args) ⇒ Object



81
82
83
# File 'lib/rquery/operation_collector.rb', line 81

def not_in(*args)
  call_in(NOT_PREFIX, *args)
end

#to_conditionsObject

return a conditions array for use with ActiveRecord.find



17
18
19
# File 'lib/rquery/operation_collector.rb', line 17

def to_conditions
  [to_s] + @values
end

#to_sObject



12
13
14
# File 'lib/rquery/operation_collector.rb', line 12

def to_s
  RQuery::Config.adapter.join(@operations)
end

#without(str) ⇒ Object



98
99
100
101
# File 'lib/rquery/operation_collector.rb', line 98

def without(str)
  @values << str
  chain :without
end

#|(second) ⇒ Object



73
74
75
# File 'lib/rquery/operation_collector.rb', line 73

def |(second)
  group :or
end