Class: RailsSimpleSearch::ConditionGroup

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

Overview

This class holds a ConditionGroup One ConditionGroup can hold one or more conditions

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ ConditionGroup

Returns a new instance of ConditionGroup.



155
156
157
158
159
160
161
# File 'lib/sql_handler.rb', line 155

def initialize(item = nil)
  if item
    @condition_item = item
  else
    @children = []
  end
end

Instance Method Details

#add(condition_group) ⇒ Object



163
164
165
166
167
# File 'lib/sql_handler.rb', line 163

def add(condition_group)
  raise "It's not allowed to add child into leaf node" if leaf?

  @children << condition_group if condition_group
end

#empty?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/sql_handler.rb', line 179

def empty?
  @children && @children.empty? ? true : false
end

#leaf?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/sql_handler.rb', line 175

def leaf?
  @condition_item ? true : false
end

#set_relation(and_or) ⇒ Object



169
170
171
172
173
# File 'lib/sql_handler.rb', line 169

def set_relation(and_or)
  raise "It's not needed to set relation for leaf node" if leaf?

  @relation = and_or
end

#to_ar_conditionObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/sql_handler.rb', line 183

def to_ar_condition
  condition = []
  if leaf?
    i = @condition_item
    condition << "#{i.field} #{i.verb}"
    condition[0] << (i.verb == 'in' ? ' (?)' : ' ?')
    condition << i.value
  else
    tmp_conditions = @children.map(&:to_ar_condition)
    tmp_condition_str = tmp_conditions.map(&:first).join(" #{@relation} ")
    condition << "(#{tmp_condition_str})"
    tmp_conditions.each do |t|
      (1..(t.length - 1)).each do |index|
        condition << t[index]
      end
    end
  end
  condition
end