Class: Castle::Log::File

Inherits:
Object
  • Object
show all
Defined in:
lib/castle/log/file.rb

Overview

Represents a single Castle data logger file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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') }
    meta = 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_savedObject (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

#notesString (readonly)

Returns Notes at the beginning of the file.

Returns:

  • (String)

    Notes at the beginning of the file



12
13
14
# File 'lib/castle/log/file.rb', line 12

def notes
  @notes
end

#sessionsArray<Session> (readonly)

Returns Sessions contained in this file.

Returns:

  • (Array<Session>)

    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.

Parameters:

  • uri

    URI to file to read

Returns:

  • (Castle::Log::File)

    loaded file if the file is a Castle log file, nil otherwise



21
22
23
# File 'lib/castle/log/file.rb', line 21

def self.castle? uri
  File.new(uri) rescue nil
end

Instance Method Details

#durationFloat

Gets the total duration of all sessions contained within.

Returns:

  • (Float)

    total duration of all sessions, in seconds



62
63
64
# File 'lib/castle/log/file.rb', line 62

def duration
  @sessions.map(&:duration).reduce(&:+)
end