Class: Grok::Pile

Inherits:
Object
  • Object
show all
Defined in:
lib/grok/c-ext/pile.rb

Instance Method Summary collapse

Constructor Details

#initializePile

Returns a new instance of Pile.



10
11
12
13
14
# File 'lib/grok/c-ext/pile.rb', line 10

def initialize
  @groks = []
  @patterns = {}
  @pattern_files = []
end

Instance Method Details

#add_pattern(name, string) ⇒ Object

see Grok#add_pattern



17
18
19
# File 'lib/grok/c-ext/pile.rb', line 17

def add_pattern(name, string)
  @patterns[name] = string
end

#add_patterns_from_file(path) ⇒ Object

see Grok#add_patterns_from_file



22
23
24
25
26
27
# File 'lib/grok/c-ext/pile.rb', line 22

def add_patterns_from_file(path)
  if !File.exists?(path)
    raise "File does not exist: #{path}"
  end
  @pattern_files << path
end

#compile(pattern) ⇒ Object

see Grok#compile



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/grok/c-ext/pile.rb', line 30

def compile(pattern)
  grok = Grok.new
  @patterns.each do |name, value|
    grok.add_pattern(name, value)
  end
  @pattern_files.each do |path|
    grok.add_patterns_from_file(path)
  end
  grok.compile(pattern)
  @groks << grok
end

#match(string) ⇒ Object

Slight difference from Grok#match in that it returns the Grok instance that matched successfully in addition to the GrokMatch result. See also: Grok#match



46
47
48
49
50
51
52
53
54
# File 'lib/grok/c-ext/pile.rb', line 46

def match(string)
  @groks.each do |grok|
    match = grok.match(string)
    if match
      return [grok, match]
    end
  end
  return false
end