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

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ File

Returns a new instance of File.



17
18
19
20
21
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
47
48
49
# File 'lib/castle/log/file.rb', line 17

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