Class: KQL::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/kql/filter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node: nil, tag: nil, matchers: []) ⇒ Filter

Returns a new instance of Filter.



5
6
7
8
9
# File 'lib/kql/filter.rb', line 5

def initialize(node: nil, tag: nil, matchers: [])
  @node = node
  @tag = tag
  @matchers = matchers
end

Instance Attribute Details

#matchersObject (readonly)

Returns the value of attribute matchers.



3
4
5
# File 'lib/kql/filter.rb', line 3

def matchers
  @matchers
end

#nodeObject (readonly)

Returns the value of attribute node.



3
4
5
# File 'lib/kql/filter.rb', line 3

def node
  @node
end

#tagObject (readonly)

Returns the value of attribute tag.



3
4
5
# File 'lib/kql/filter.rb', line 3

def tag
  @tag
end

Instance Method Details

#==(other) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/kql/filter.rb', line 11

def ==(other)
  return false unless other.is_a?(Filter)

  other.node == node &&
    other.tag == tag &&
    other.matchers == matchers
end

#execute(context) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/kql/filter.rb', line 19

def execute(context)
  selected_nodes = []

  context.selected_nodes.flat_map do |n|
    selected_nodes << n if match?(n.node)
    selected_nodes += filter_nodes(n.node.children, n.node) unless n.stop
  end

  Query::Context.new(selected_nodes)
end