Class: Rbr::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/rbr/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, condition) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rbr/query.rb', line 11

def initialize(matcher, condition)
  @matcher = matcher
  @condition = condition

  if matcher == :comment
    alias run run_comments
  else
    alias run run_tree

    if condition.is_a?(String) && condition.start_with?(":")
      @condition = condition[1..].to_sym
    end
  end
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



9
10
11
# File 'lib/rbr/query.rb', line 9

def condition
  @condition
end

#matcherObject (readonly)

Returns the value of attribute matcher.



9
10
11
# File 'lib/rbr/query.rb', line 9

def matcher
  @matcher
end

Instance Method Details

#run_comments(_node, comments) ⇒ Object



26
27
28
29
# File 'lib/rbr/query.rb', line 26

def run_comments(_node, comments)
  comments.select { |comment| comment.text.match?(condition) }
          .map { |comment| CommentNode.new(comment) }
end

#run_tree(node, _comments = []) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/rbr/query.rb', line 31

def run_tree(node, _comments = [])
  node = Node.new(node) unless node.is_a?(Node)
  return [] if node.nil?

  node_matches = Matchers.match(node, matcher, condition)
  found_children = node.children.map { |child| run(child) }

  (node_matches ? [node] : []) + found_children.flatten
end