Class: LogfileInterval::LineParser::Base
- Inherits:
-
Object
- Object
- LogfileInterval::LineParser::Base
- Defined in:
- lib/logfile_interval/line_parser/base.rb
Class Attribute Summary collapse
-
.regex ⇒ Object
readonly
Returns the value of attribute regex.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
- .add_column(options) ⇒ Object
- .columns ⇒ Object
- .create_record(line) ⇒ Object
- .parse(line) ⇒ Object
- .set_regex(regex) ⇒ Object
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#initialize(line) ⇒ Base
constructor
A new instance of Base.
- #time ⇒ Object
- #valid? ⇒ Boolean
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
.regex ⇒ Object (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
#data ⇒ Object (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() name = .fetch(:name) pos = .fetch(:pos) conversion = .fetch(:conversion, :string) group_by = .fetch(:group_by, nil) aggregator = .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 |
.columns ⇒ Object
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
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, | val = match_data[[:pos]] data[name] = convert(val, [: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 |
#time ⇒ Object
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 |