class CompoundPredicate

Public Class Methods

new(operator, subpredicates) click to toggle source
# File lib/predicates/compound_predicate.rb, line 7
def initialize(operator, subpredicates)
  @operator = operator
  @subpredicates = subpredicates.map { |each| PredicateLiteral.parse(each) }
end
parse(array) click to toggle source
# File lib/predicates/compound_predicate.rb, line 3
def self.parse(array)
  self.new(array[1], array[2..-1])
end

Public Instance Methods

build_arel(arel_table) click to toggle source
# 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