Module: LogfileInterval::ParsedLine::Parser

Included in:
Base
Defined in:
lib/logfile_interval/parsed_line/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#regexObject (readonly)

Returns the value of attribute regex.



6
7
8
# File 'lib/logfile_interval/parsed_line/parser.rb', line 6

def regex
  @regex
end

Instance Method Details

#add_column(options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/logfile_interval/parsed_line/parser.rb', line 15

def add_column(options)
  validate_column_options(options)
  options = sanitize_column_options(options)

  name = options[:name]
  columns[name] = options

  define_method(name) do
    @data[name]
  end
end

#columnsObject



7
8
9
# File 'lib/logfile_interval/parsed_line/parser.rb', line 7

def columns
  @columns ||= {}
end

#create_record(line) ⇒ Object



42
43
44
45
46
# File 'lib/logfile_interval/parsed_line/parser.rb', line 42

def create_record(line)
  record = new(line)
  return record if record.valid?
  return nil
end

#each(&block) ⇒ Object



54
55
56
# File 'lib/logfile_interval/parsed_line/parser.rb', line 54

def each(&block)
  columns.each(&block)
end

#parse(line) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logfile_interval/parsed_line/parser.rb', line 27

def parse(line)
  raise ConfigurationError, 'There must be at least 1 configured column' unless columns.any?
  raise ConfigurationError, 'A regex must be set' unless regex

  match_data = regex.match(line)
  return nil unless match_data

  data = {}
  columns.each do |name, options|
    val = match_data[options[:pos]]
    data[name] = convert(val, options[:conversion])
  end
  data
end

#set_column_custom_options(column_name, options) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
# File 'lib/logfile_interval/parsed_line/parser.rb', line 48

def set_column_custom_options(column_name, options)
  raise ArgumentError, "Invalid column name: #{column_name}" unless columns.has_key?(column_name)
  columns[column_name][:custom_options] = options
end

#set_regex(regex) ⇒ Object



11
12
13
# File 'lib/logfile_interval/parsed_line/parser.rb', line 11

def set_regex(regex)
  @regex = regex
end