Class: Raykit::Log

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

Class Method Summary collapse

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

Class Method Details

.name_value_pair(symbol, the_binding) ⇒ Object



53
54
55
# File 'lib/raykit/log.rb', line 53

def self.name_value_pair(symbol, the_binding)
  [symbol.to_s, eval(symbol.to_s, the_binding)]
end

.show_table(symbols) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/raykit/log.rb', line 57

def self.show_table(symbols)
  max_name_width = 0
  max_value_width = 0
  symbols.each { |s|
    nvp = name_value_pair(s, binding)
    name = nvp[0]
    value = nvp[1]
    max_name_width = name.length if (name.length > max_name_width)
    if (!value.nil?)
      max_value_width = value.length if (value.length > max_value_width)
    end
  }
  header = "  =".ljust(max_name_width + max_value_width + 5, "=")
  puts header
  symbols.each { |s|
    nvp = name_value_pair(s, binding)
    name = nvp[0].rjust(max_name_width, " ")
    value = nvp[1]
    puts "  " + Rainbow(name).cyan + " | " + Rainbow("#{value}").white.bold
  }
  puts header
end

.show_value(name, value) ⇒ Object



48
49
50
51
# File 'lib/raykit/log.rb', line 48

def self.show_value(name, value)
  puts "  " + Rainbow(name).cyan + " " + Rainbow("#{value}").white.bold
  MARKDOWN.puts("#{name} #{value}")
end

.start_task(task_name) ⇒ Object



43
44
45
46
# File 'lib/raykit/log.rb', line 43

def self.start_task(task_name)
  puts Rainbow(": #{task_name}").blue.bright
  MARKDOWN.puts(": #{task_name}")
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