Class: Raykit::Log

Inherits:
Hash
  • Object
show all
Defined in:
lib/raykit/log.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Log

Returns a new instance of Log.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/raykit/log.rb', line 7

def initialize(filename)
  @filename = filename
  dir = File.dirname(@filename)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  if File.exist?(@filename)
    begin
      data = JSON.parse(File.read(filename))
      data.each do |k, v|
        self[k] = v
      end
    rescue StandardError
    end
  end
end

Instance Method Details

#get_command_time(command) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/raykit/log.rb', line 35

def get_command_time(command)
  if key?("command_times")
    command_times = self["command_times"]
    return DateTime.parse(command_times[command]) if command_times.key?(command)
  end
  nil
end

#saveObject



22
23
24
# File 'lib/raykit/log.rb', line 22

def save
  File.open(@filename, "w") { |f| f.write(JSON.generate(self)) }
end

#update_command_time(command, timestamp) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/raykit/log.rb', line 26

def update_command_time(command, timestamp)
  command_times = {}
  command_times = self["command_times"] if key?("command_times")
  command_times.delete(command) if command_times.key?(command)
  command_times[command] = timestamp.iso8601
  self["command_times"] = command_times
  save
end