lib/raykit/log.rb



require 'json'
module Raykit
    class Log < Hash
        @filename
        def initialize(filename)
            @filename=filename
            dir=File.dirname(@filename)
            FileUtils.mkdir_p(dir) if(!Dir.exist?(dir))
            if(File.exist?(@filename))
                data=JSON.parse(File.read(filename))
                data.each{|k,v|
                    self[k] = v
                }
            end
        end

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

        def update_command_time(command,timestamp)
            command_times = Hash.new()
            command_times = self["command_times"] if(self.has_key?("command_times"))
            command_times.delete(command) if(command_times.has_key?(command))
            command_times[command] = timestamp.iso8601()
            self["command_times"] = command_times
            save
        end

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