class Praxis::Extensions::AttributeFiltering::FilteringParams::ConditionGroup

An Object that represents an AST tree for either an OR or an AND conditions to be applied to its items children

Attributes

associated_query[RW]
items[R]
parent_group[RW]
type[R]

Public Class Methods

compress_tree(node:, operator:) click to toggle source

Given a binary tree of operand conditions, transform it to a multi-leaf tree where a single condition node has potentially multiple subtrees for the same operation (instead of 2) For example (&, (&, a, b), (|, c, d)) => (&, a, b, (|, c, d))

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 152
def self.compress_tree(node:, operator:)
  return [node] if node[:triad]

  # It is an op node
  if node[:o] == operator
    # compatible op as parent, collect my compacted children and return them up skipping my op
    resultl = compress_tree(node: node[:l], operator: operator)
    resultr = compress_tree(node: node[:r], operator: operator)
    resultl + resultr
  else
    collected = compress_tree(node: node, operator: node[:o])
    [{ op: node[:o], items: collected }]
  end
end
load(node) click to toggle source
# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 111
def self.load(node)
  if node[:o]
    compactedl = compress_tree(node: node[:l], operator: node[:o])
    compactedr = compress_tree(node: node[:r], operator: node[:o])
    compacted = { op: node[:o], items: compactedl + compactedr }

    loaded = ConditionGroup.new(**compacted, parent_group: nil)
  else
    loaded = Condition.new(triad: node[:triad], parent_group: nil)
  end
  loaded
end
new(op:, items:, parent_group:) click to toggle source

rubocop:disable Naming/MethodParameterName

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 125
def initialize(op:, items:, parent_group:)
  @type = op.to_s == '&' ? :and : :or
  @items = items.map do |item|
    if item[:op]
      ConditionGroup.new(**item, parent_group: self)
    else
      Condition.new(triad: item[:triad], parent_group: self)
    end
  end
  @parent_group = parent_group
end

Public Instance Methods

dump() click to toggle source

rubocop:enable Naming/MethodParameterName

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 138
def dump
  "( #{@items.map(&:dump).join(" #{type.upcase} ")} )"
end
flattened_conditions() click to toggle source

Returns an array with flat conditions from all child triad conditions

# File lib/praxis/extensions/attribute_filtering/filters_parser.rb, line 143
def flattened_conditions
  @items.inject([]) do |accum, item|
    accum + item.flattened_conditions
  end
end