Class: RailsSimpleSearch::ConditionGroup

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

Instance Method Summary collapse

Constructor Details

#initialize(item = nil) ⇒ ConditionGroup

Returns a new instance of ConditionGroup.



137
138
139
140
141
142
143
# File 'lib/sql_handler.rb', line 137

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

Instance Method Details

#add(condition_group) ⇒ Object



145
146
147
148
149
# File 'lib/sql_handler.rb', line 145

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)


161
162
163
# File 'lib/sql_handler.rb', line 161

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

#leaf?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/sql_handler.rb', line 157

def leaf?
  @condition_item ? true : false
end

#set_relation(and_or) ⇒ Object



151
152
153
154
155
# File 'lib/sql_handler.rb', line 151

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/sql_handler.rb', line 165

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