Class: Indy::LogDefinition
- Inherits:
-
Object
- Object
- Indy::LogDefinition
- Defined in:
- lib/indy/log_definition.rb
Instance Attribute Summary collapse
-
#entry_fields ⇒ Object
Returns the value of attribute entry_fields.
-
#entry_regexp ⇒ Object
Returns the value of attribute entry_regexp.
-
#multiline ⇒ Object
Returns the value of attribute multiline.
-
#time_format ⇒ Object
Returns the value of attribute time_format.
Instance Method Summary collapse
-
#assert_valid_field_list(values) ⇒ Object
Ensure number of fields is expected.
-
#create_struct(entry_hash) ⇒ Object
Return a Struct::Entry object from a hash of values from a log entry.
-
#define_struct ⇒ Object
Define Struct::Entry.
-
#entry_hash(values) ⇒ Object
Convert log entry into hash.
-
#initialize(args = :default) ⇒ LogDefinition
constructor
A new instance of LogDefinition.
-
#parse_entry(raw_entry) ⇒ Object
Return a hash of field=>value pairs for the log entry.
-
#parse_entry_captures(capture_array) ⇒ Object
Return a hash of field=>value pairs for the array of captured values from a log entry.
- #parse_enumerable_params(args) ⇒ Object
- #set_defaults ⇒ Object
Constructor Details
#initialize(args = :default) ⇒ LogDefinition
Returns a new instance of LogDefinition.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/indy/log_definition.rb', line 7 def initialize(args=:default) case args when :default, {} params_hash = set_defaults when Array, Hash params_hash = parse_enumerable_params(args) end raise ArgumentError, "Values for entry_regexp and/or entry_fields were not supplied" unless (params_hash[:entry_fields] && params_hash[:entry_regexp]) if params_hash[:multiline] @entry_regexp = Regexp.new(params_hash[:entry_regexp], Regexp::MULTILINE) @multiline = true else @entry_regexp = Regexp.new(params_hash[:entry_regexp]) end @entry_fields = params_hash[:entry_fields] @time_format = params_hash[:time_format] || Indy::LogFormats::DEFAULT_DATE_TIME # '%Y-%m-%d %H:%M:%S' define_struct end |
Instance Attribute Details
#entry_fields ⇒ Object
Returns the value of attribute entry_fields.
5 6 7 |
# File 'lib/indy/log_definition.rb', line 5 def entry_fields @entry_fields end |
#entry_regexp ⇒ Object
Returns the value of attribute entry_regexp.
5 6 7 |
# File 'lib/indy/log_definition.rb', line 5 def entry_regexp @entry_regexp end |
#multiline ⇒ Object
Returns the value of attribute multiline.
5 6 7 |
# File 'lib/indy/log_definition.rb', line 5 def multiline @multiline end |
#time_format ⇒ Object
Returns the value of attribute time_format.
5 6 7 |
# File 'lib/indy/log_definition.rb', line 5 def time_format @time_format end |
Instance Method Details
#assert_valid_field_list(values) ⇒ Object
Ensure number of fields is expected
107 108 109 110 111 112 113 |
# File 'lib/indy/log_definition.rb', line 107 def assert_valid_field_list(values) if values.length == @entry_fields.length + 1 # values also includes raw_entry @field_list_is_valid = true else raise Indy::Source::FieldMismatchException, "Number of expected fields does not match those captured via the regexp pattern.\nThe expected fields are:\n=> #{@entry_fields.join("\n=> ")}\nThe log entry and captured fields are:\n=> #{values.join("\n=> ")}" end end |
#create_struct(entry_hash) ⇒ Object
Return a Struct::Entry object from a hash of values from a log entry
51 52 53 54 55 |
# File 'lib/indy/log_definition.rb', line 51 def create_struct( entry_hash ) values = entry_hash.keys.sort_by{|entry|entry.to_s}.collect {|key| entry_hash[key]} result = Struct::Entry.new( *values ) result end |
#define_struct ⇒ Object
Define Struct::Entry. Ignore warnings.
60 61 62 63 64 65 66 |
# File 'lib/indy/log_definition.rb', line 60 def define_struct fields = (@entry_fields + [:raw_entry]).sort_by{|key|key.to_s} verbose = $VERBOSE $VERBOSE = nil Struct.new( "Entry", *fields ) $VERBOSE = verbose end |
#entry_hash(values) ⇒ Object
Convert log entry into hash
71 72 73 74 75 76 77 |
# File 'lib/indy/log_definition.rb', line 71 def entry_hash(values) assert_valid_field_list(values) unless @field_list_is_valid # just do it once raw_entry = values.shift hash = Hash[ *@entry_fields.zip( values ).flatten ] hash[:raw_entry] = raw_entry.strip hash end |
#parse_entry(raw_entry) ⇒ Object
Return a hash of field=>value pairs for the log entry
85 86 87 88 89 90 91 |
# File 'lib/indy/log_definition.rb', line 85 def parse_entry(raw_entry) match_data = /#{@entry_regexp}/.match(raw_entry) return nil unless match_data values = match_data.captures values.shift if @multiline entry_hash([raw_entry, values].flatten) end |
#parse_entry_captures(capture_array) ⇒ Object
Return a hash of field=>value pairs for the array of captured values from a log entry
98 99 100 101 102 |
# File 'lib/indy/log_definition.rb', line 98 def parse_entry_captures( capture_array ) entire_entry = capture_array.shift values = capture_array entry_hash([entire_entry, values].flatten) end |
#parse_enumerable_params(args) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/indy/log_definition.rb', line 34 def parse_enumerable_params(args) params_hash = {} params_hash.merge!(args) if args.keys.include? :log_format # support 0.3.4 params params_hash[:entry_regexp] = args[:log_format][0] params_hash[:entry_fields] = args[:log_format][1..-1] params_hash.delete :log_format end params_hash end |
#set_defaults ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/indy/log_definition.rb', line 26 def set_defaults params_hash = {} params_hash[:entry_regexp] = Indy::LogFormats::DEFAULT_ENTRY_REGEXP params_hash[:entry_fields] = Indy::LogFormats::DEFAULT_ENTRY_FIELDS params_hash[:time_format] = Indy::LogFormats::DEFAULT_DATE_TIME params_hash end |