Class: Castle::Log::File
- Inherits:
-
Object
- Object
- Castle::Log::File
- Defined in:
- lib/castle/log/file.rb
Overview
Represents a single Castle data logger file.
Instance Attribute Summary collapse
-
#date_saved ⇒ Object
readonly
Returns the value of attribute date_saved.
-
#notes ⇒ String
readonly
Notes at the beginning of the file.
-
#sessions ⇒ Array<Session>
readonly
Sessions contained in this file.
Class Method Summary collapse
-
.castle?(uri) ⇒ Castle::Log::File
Determines if the file at the given URI is a Castle log file.
Instance Method Summary collapse
-
#duration ⇒ Float
Gets the total duration of all sessions contained within.
-
#initialize(uri) ⇒ File
constructor
A new instance of File.
Constructor Details
#initialize(uri) ⇒ File
Returns a new instance of File.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/castle/log/file.rb', line 25 def initialize uri @sessions = [] open(uri, 'rb') do |file| i = 0 in_comment = false lines = file.readlines.map(&:strip).group_by do |line| if line.start_with?('#') && !in_comment in_comment = true i += 1 elsif !line.start_with?('#') && in_comment in_comment = false end i end # session 1 has some extra metadata, scrape that out first notes = lines[1].select { |line| line.start_with?('# Note') } = lines[1].select { |line| line.start_with?('# Graph Data') } @sessions = extract_sessions(lines) @notes = (notes.map { |line| /Note -(?<note>.*)$/.match(line)[:note].strip }).join("\n") # TODO validate expected number of sessions from meta # TODO extract date_saved end rescue raise ArgumentError, 'File does not appear to be an Castle data log' end |
Instance Attribute Details
#date_saved ⇒ Object (readonly)
Returns the value of attribute date_saved.
9 10 11 |
# File 'lib/castle/log/file.rb', line 9 def date_saved @date_saved end |
#notes ⇒ String (readonly)
Returns Notes at the beginning of the file.
12 13 14 |
# File 'lib/castle/log/file.rb', line 12 def notes @notes end |
#sessions ⇒ Array<Session> (readonly)
Returns Sessions contained in this file.
15 16 17 |
# File 'lib/castle/log/file.rb', line 15 def sessions @sessions end |
Class Method Details
.castle?(uri) ⇒ Castle::Log::File
Determines if the file at the given URI is a Castle log file.
21 22 23 |
# File 'lib/castle/log/file.rb', line 21 def self.castle? uri File.new(uri) rescue nil end |
Instance Method Details
#duration ⇒ Float
Gets the total duration of all sessions contained within.
62 63 64 |
# File 'lib/castle/log/file.rb', line 62 def duration @sessions.map(&:duration).reduce(&:+) end |