Class: Yayaml::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/yayaml/matcher.rb

Constant Summary collapse

BOLD_RED =
"\e[1m\e[38;5;1m"
RESET =
"\e[0m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, match_path: false, ignore_case: false) ⇒ Matcher

Returns a new instance of Matcher.



8
9
10
11
12
13
14
# File 'lib/yayaml/matcher.rb', line 8

def initialize(pattern, match_path: false, ignore_case: false)
  @pattern = Regexp.compile(pattern)
  @match_path = match_path
  @ignore_case = ignore_case

  @nodes = []
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



6
7
8
# File 'lib/yayaml/matcher.rb', line 6

def nodes
  @nodes
end

Instance Method Details

#on_node(keys, value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/yayaml/matcher.rb', line 16

def on_node(keys, value)
  @nodes << [keys, value]

  path = keys.join(".")

  if @match_path
    if (match = path.match(@pattern))
      path_matched = path.sub(@pattern, "#{BOLD_RED}#{match}#{RESET}")
      # puts "#{path_matched}:#{@line}: #{value}"
      puts "#{@filename}:#{@line} #{path_matched}: #{value}"
    end
  else
    if (match = value.match(@pattern))
      value_matched = value.sub(@pattern, "#{BOLD_RED}#{match}#{RESET}")
      # puts "#{path}:#{@line}: #{value_matched}"
      puts "#{@filename}:#{@line} #{path}: #{value_matched}"
    end
  end
end