Class: Praxis::Extensions::AttributeFiltering::FilterTreeNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_filters, path: []) ⇒ FilterTreeNode

Parsed_filters is an Array of X, op: Y, value: Z … exactly the format of the FilteringParams.load method



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/praxis/extensions/attribute_filtering/filter_tree_node.rb', line 10

def initialize(parsed_filters, path: [])
  @path = path # Array that marks the tree 'path' to this node (with respect to the absolute root)
  @conditions = [] # Conditions to apply directly to this node
  @children = {} # Hash with a new NodeTree object value, keyed by name
  children_data = {} # Hash with keys as names of the first level component of the children nodes (and values as array of matching filters)
  parsed_filters.map do |hash|
    *components = hash[:name].to_s.split('.')
    next if components.empty?

    if components.size == 1
      @conditions << hash.slice(:name, :op, :value, :fuzzy, :node_object)
    else
      children_data[components.first] ||= []
      children_data[components.first] << hash
    end
  end
  # An array of FilterTreeNodes corresponding to each children
  @children = children_data.each_with_object({}) do |(name, arr), hash|
    sub_filters = arr.map do |item|
      _parent, *rest = item[:name].to_s.split('.')
      item.merge(name: rest.join('.'))
    end
    hash[name] = self.class.new(sub_filters, path: path + [name])
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



7
8
9
# File 'lib/praxis/extensions/attribute_filtering/filter_tree_node.rb', line 7

def children
  @children
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



7
8
9
# File 'lib/praxis/extensions/attribute_filtering/filter_tree_node.rb', line 7

def conditions
  @conditions
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/praxis/extensions/attribute_filtering/filter_tree_node.rb', line 7

def path
  @path
end