Class: CompoundPredicate

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator, subpredicates) ⇒ CompoundPredicate

Returns a new instance of CompoundPredicate.



7
8
9
10
# File 'lib/predicates/compound_predicate.rb', line 7

def initialize(operator, subpredicates)
  @operator = operator
  @subpredicates = subpredicates.map { |each| PredicateLiteral.parse(each) }
end

Class Method Details

.parse(array) ⇒ Object



3
4
5
# File 'lib/predicates/compound_predicate.rb', line 3

def self.parse(array)
  self.new(array[1], array[2..-1])
end

Instance Method Details

#build_arel(arel_table) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/predicates/compound_predicate.rb', line 12

def build_arel(arel_table)
  return BooleanExpression::TRUE.build_arel(arel_table) if @subpredicates.empty?

  lhs = @subpredicates.first.build_arel(arel_table)
  case @operator
    when "&"
      @subpredicates[1..-1].each do |each|
        rhs = each.build_arel(arel_table)
        lhs = lhs.and(rhs)
      end
    when "|"
      @subpredicates[1..-1].each do |each|
        rhs = each.build_arel(arel_table)
        lhs = lhs.or(rhs)
      end
    when "!"
      lhs = lhs.not()
    else
      raise "Unknown operator #{@operator}"
  end
  lhs
end