Class: Praxis::Extensions::AttributeFiltering::FilteringParams::ConditionGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/extensions/attribute_filtering/filters_parser.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op:, items:, parent_group:) ⇒ ConditionGroup

rubocop:disable Naming/MethodParameterName



125
126
127
128
129
130
131
132
133
134
135
# 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

Instance Attribute Details

#associated_queryObject

Metadata to be used by whomever is manipulating this



109
110
111
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 109

def associated_query
  @associated_query
end

#itemsObject (readonly)

Returns the value of attribute items.



108
109
110
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 108

def items
  @items
end

#parent_groupObject

Metadata to be used by whomever is manipulating this



109
110
111
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 109

def parent_group
  @parent_group
end

#typeObject (readonly)

Returns the value of attribute type.



108
109
110
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 108

def type
  @type
end

Class Method Details

.compress_tree(node:, operator:) ⇒ Object

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))



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# 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

Instance Method Details

#dumpObject

rubocop:enable Naming/MethodParameterName



138
139
140
# File 'lib/praxis/extensions/attribute_filtering/filters_parser.rb', line 138

def dump
  "( #{@items.map(&:dump).join(" #{type.upcase} ")} )"
end

#flattened_conditionsObject

Returns an array with flat conditions from all child triad conditions



143
144
145
146
147
# 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