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.



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
58
59
# File 'lib/castle/log/file.rb', line 27

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) ⇒ Boolean

Determines if the file at the given URI is a Castle log file. If one intends to read the file, simply using #new should be preferred in favor of this, catching any errors that may be raised.

Parameters:

  • uri

    URI to file to read

Returns:

  • (Boolean)

    true if the file is a Castle log file, false otherwise



23
24
25
# File 'lib/castle/log/file.rb', line 23

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