class Verkilo::Log

Public Class Methods

new(type, root_dir, offset) click to toggle source
# File lib/verkilo/log.rb, line 5
def initialize(type, root_dir, offset)
  @type = type
  @root_dir = root_dir
  @data = YAML.load(read_file) || {}
  @today = Time.now.getlocal(offset).strftime("%F")
end

Public Instance Methods

data() click to toggle source
# File lib/verkilo/log.rb, line 36
def data
  @data
end
data=(h) click to toggle source
# File lib/verkilo/log.rb, line 31
def data=(h)
  # Don't clobber existing data.
  h.merge!(@data[@today].to_h) if @data.keys.include?(@today)
  @data.merge!({@today => h})
end
delta!(target='_total') click to toggle source
# File lib/verkilo/log.rb, line 11
def delta!(target='_total')
  change = 0
  last = nil
  keys = @data.keys
  ckey = target.sub(/_/,'_Δ')
  until keys.empty? do
    k = keys.shift
    total!(k)
    now = @data[k][target] || 0
    change = (last.nil?) ? 0 : now - last
    @data[k].merge!({ckey => change})
    last = now
  end
end
filename() click to toggle source
# File lib/verkilo/log.rb, line 46
def filename
  File.join([@root_dir, '.verkilo', "#{@type}.yml"])
end
total!(k=nil) click to toggle source
# File lib/verkilo/log.rb, line 25
def total!(k=nil)
  k ||= @today
  @data[k]["_total"] = @data[k].map {
    |k,v| (/^_/.match?(k)) ? 0 : v
  }.inject(0, :+)
end
write() click to toggle source
# File lib/verkilo/log.rb, line 39
def write
  fname = self.filename
  FileUtils.mkdir_p(File.dirname(fname))
  f = File.open(fname,'w')
  f.write(@data.to_yaml)
  f.close
end

Private Instance Methods

read_file() click to toggle source
# File lib/verkilo/log.rb, line 50
def read_file
  contents = "---"
  if File.exist?(self.filename)
    f = File.open(self.filename,'r')
    contents = f.read()
    f.close
  end
  return contents
end