class Kameleoon::Targeting::Tree

Attributes

condition[RW]
left_child[RW]
or_operator[RW]
right_child[RW]

Public Class Methods

new(or_operator = nil, left_child = nil, right_child = nil, condition = nil) click to toggle source
# File lib/kameleoon/targeting/models.rb, line 67
def initialize(or_operator = nil, left_child = nil, right_child = nil, condition = nil)
  @or_operator = Marshal.load(Marshal.dump(or_operator))
  @left_child = left_child
  @right_child = right_child
  @condition = condition
end

Public Instance Methods

check(datas) click to toggle source
# File lib/kameleoon/targeting/models.rb, line 74
def check(datas)
  unless @condition.nil?
    is_targeted = check_condition(datas)
  else
    if @left_child.nil?
      is_left_child_targeted = true
    else
      is_left_child_targeted = @left_child.check(datas)
    end

    if is_left_child_targeted.nil?
      has_to_compute_right_child = true
    else
      has_to_compute_right_child = (is_left_child_targeted != @or_operator)
    end

    # Compute right child tree
    is_right_child_targeted = nil
    if has_to_compute_right_child
      if @right_child.nil?
        is_right_child_targeted = true
      else
        is_right_child_targeted = @right_child.check(datas)
      end
    end

    # Computing results
    if is_left_child_targeted.nil?
      if is_right_child_targeted == @or_operator
        is_targeted = Marshal.load(Marshal.dump(@or_operator)) #Deep copy
      else
        is_targeted = nil
      end
    else
      if is_left_child_targeted == @or_operator
        is_targeted = Marshal.load(Marshal.dump(@or_operator)) #Deep copy
      else
        if is_right_child_targeted == true
          is_targeted = true
        elsif is_right_child_targeted == false
          is_targeted = false
        else
          is_targeted = nil
        end
      end
    end
  end
  Marshal.load(Marshal.dump(is_targeted)) #Deep copy
end
check_condition(datas, condition = @condition) click to toggle source
# File lib/kameleoon/targeting/models.rb, line 124
def check_condition(datas, condition = @condition)
  if condition.nil?
    is_targeted = true
  else
    is_targeted = condition.check(datas)
    unless condition.include
      if is_targeted.nil?
        is_targeted = true
      else
        is_targeted = !is_targeted
      end
    end
  end
  Marshal.load(Marshal.dump(is_targeted)) #Deep copy
end
to_s() click to toggle source
# File lib/kameleoon/targeting/models.rb, line 51
def to_s
  print("or_operator: " + @or_operator.to_s)
  print("\n")
  print("condition: " + @condition.to_s)
  unless @left_child.nil?
    print("\n")
    print("Left child:\n    ")
    @left_child.to_s
  end
  unless @right_child.nil?
    print("\n")
    print("right child:\n    ")
    @right_child.to_s
  end
end