Class: LogfileInterval::LineParser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/logfile_interval/line_parser/base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line) ⇒ Base



84
85
86
87
# File 'lib/logfile_interval/line_parser/base.rb', line 84

def initialize(line)
  @data = self.class.parse(line)
  @valid = @data ? true : false
end

Class Attribute Details

.regexObject (readonly)

Returns the value of attribute regex.



12
13
14
# File 'lib/logfile_interval/line_parser/base.rb', line 12

def regex
  @regex
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



9
10
11
# File 'lib/logfile_interval/line_parser/base.rb', line 9

def data
  @data
end

Class Method Details

.add_column(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logfile_interval/line_parser/base.rb', line 22

def add_column(options)
  name          = options.fetch(:name)
  pos           = options.fetch(:pos)
  conversion    = options.fetch(:conversion, :string)
  group_by      = options.fetch(:group_by, nil)
  aggregator    = options.fetch(:aggregator)
  unless AGGREGATION_FUNCTIONS.include?(aggregator)
    raise ArgumentError, "aggregator must be one of #{AGGREGATION_FUNCTIONS.join(', ')}"
  end

  name      = name.to_sym
  group_by  = group_by.to_sym unless group_by.nil?

  if aggregator == :count && group_by && group_by != name
    aggregator = :group_and_count
  end

  aggregator = Aggregator.klass(aggregator)
  columns[name] = { :pos => pos, :aggregator => aggregator, :conversion => conversion }
  columns[name][:group_by] = group_by

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

.columnsObject



14
15
16
# File 'lib/logfile_interval/line_parser/base.rb', line 14

def columns
  @columns ||= {}
end

.create_record(line) ⇒ Object



64
65
66
67
68
# File 'lib/logfile_interval/line_parser/base.rb', line 64

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

.parse(line) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/logfile_interval/line_parser/base.rb', line 48

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
  return nil unless match_data.size >= columns.size+1

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

.set_regex(regex) ⇒ Object



18
19
20
# File 'lib/logfile_interval/line_parser/base.rb', line 18

def set_regex(regex)
  @regex = regex
end

Instance Method Details

#[](name) ⇒ Object



97
98
99
# File 'lib/logfile_interval/line_parser/base.rb', line 97

def [](name)
  @data[name]
end

#timeObject

Raises:

  • (NotImplemented)


93
94
95
# File 'lib/logfile_interval/line_parser/base.rb', line 93

def time
  raise NotImplemented
end

#valid?Boolean



89
90
91
# File 'lib/logfile_interval/line_parser/base.rb', line 89

def valid?
  @valid
end