Class: DataFile3

Inherits:
Object
  • Object
show all
Defined in:
lib/Framework/DataFile3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, comment = nil, debug = false) ⇒ DataFile3

Returns a new instance of DataFile3.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/Framework/DataFile3.rb', line 6

def initialize(filename, comment = nil, debug = false)
  @filename = filename
  @debug = debug

  if File.exist?(filename)
    puts "File exist #{filename}" if @debug
  else
    File.open(@filename, 'a') do |file|
      puts "File exist #{filename}, putting version information" if @debug
      file.write("### file format:3.0 ###\n")
    end

    unless comment.nil?
      File.open(@filename, 'a') do |file|
        file.write("### comment:#{comment} ###\n")
      end
    end
  end
end

Instance Attribute Details

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



4
5
6
# File 'lib/Framework/DataFile3.rb', line 4

def debug=(value)
  @debug = value
end

#filenameObject (readonly)

Returns the value of attribute filename.



3
4
5
# File 'lib/Framework/DataFile3.rb', line 3

def filename
  @filename
end

Instance Method Details

#list_commandsObject Also known as: ListCommands



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/Framework/DataFile3.rb', line 35

def list_commands
  result = {}

  file = File.new(@filename, 'r')
  while (line = file.gets)
    if match = /### command:(.*)### time:(.*) ### timestamp: (\d+) ###/.match(line)
      result[match[1]] = [] if result[match[1]].nil?
      result[match[1]].push(match[3])
    end
  end

  result
end

#read(command) ⇒ Object Also known as: Read



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/Framework/DataFile3.rb', line 50

def read(command)
  result = {}
  insite_command = 0
  timestamp = nil

  file = File.new(@filename, 'r')
  while (line = file.gets)

    if match = /### command:(.*) ### time:(.*) ### timestamp: (\d+) ###/.match(line)
      match[1] == command ? insite_command = 1 : insite_command = 0
      timestamp = match[3]
      next
    end

    if insite_command == 1
      if result[timestamp].nil?
        result[timestamp] = line.delete("\r")
      else
        result[timestamp] += line.delete("\r")
      end
    end
  end
  file.close

  result
end

#read_simple(command) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/Framework/DataFile3.rb', line 78

def read_simple(command)

  data_string = ''
  data = read(command)

  if data.count == 1
    data_string = data[data.keys[0]]
    return data_string
  else
    return false
  end

end

#write(command, value, confirm = false) ⇒ Object Also known as: Write



26
27
28
29
30
31
32
# File 'lib/Framework/DataFile3.rb', line 26

def write(command, value, confirm = false)
  File.open(@filename, 'a') do |file|
    file.write("### command:#{command} ### time:#{Time.now.strftime("%Y-%m-%d_%H:%M:%S")} ### timestamp: #{Time.now.to_i} ###\n")
    file.write(value)
    puts STDERR '.' if confirm
  end
end