Class: Rusql::ComplexCondition

Inherits:
Condition show all
Defined in:
lib/rusql/complex_condition.rb

Constant Summary collapse

TYPES =
%i(
  and
  or
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComplexCondition

Returns a new instance of ComplexCondition.



11
12
13
# File 'lib/rusql/complex_condition.rb', line 11

def initialize
  @conditions = []
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



4
5
6
# File 'lib/rusql/complex_condition.rb', line 4

def conditions
  @conditions
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/rusql/complex_condition.rb', line 3

def type
  @type
end

Instance Method Details

#add_condition(c) ⇒ Object

Raises:



15
16
17
18
19
# File 'lib/rusql/complex_condition.rb', line 15

def add_condition(c)
  raise TypeException.new(Condition, c.class) unless c.is_a?(Condition)

  @conditions << c
end

#and(condition) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rusql/complex_condition.rb', line 27

def and(condition)
  new_condition = ComplexCondition.new
  new_condition.type = :and

  if self.type == :and
    self.conditions.each do |c|
      new_condition.add_condition(c)
    end
  else
    new_condition.add_condition(self)
  end
  new_condition.add_condition(condition)

  new_condition
end

#or(condition) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rusql/complex_condition.rb', line 43

def or(condition)
  new_condition = ComplexCondition.new
  new_condition.type = :or

  if self.type == :or
    self.conditions.each do |c|
      new_condition.add_condition(c)
    end
  else
    new_condition.add_condition(self)
  end
  new_condition.add_condition(condition)

  new_condition
end

#to_s(indent_level: 1) ⇒ Object



59
60
61
62
63
# File 'lib/rusql/complex_condition.rb', line 59

def to_s(indent_level: 1)
  indent = "  "*indent_level
  indent_out = "  "*(indent_level-1)
  "(\n" + indent + self.conditions.map{ |c| c.to_s(indent_level: indent_level+1) }.join("\n#{indent}#{self.type.to_s.upcase} ") + "\n#{indent_out})"
end