Class: Verkilo::Log
- Inherits:
-
Object
- Object
- Verkilo::Log
- Defined in:
- lib/verkilo/log.rb
Instance Method Summary collapse
- #data ⇒ Object
- #data=(h) ⇒ Object
- #delta!(target = '_total') ⇒ Object
- #filename ⇒ Object
-
#initialize(type, root_dir, offset) ⇒ Log
constructor
A new instance of Log.
- #total!(k = nil) ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(type, root_dir, offset) ⇒ Log
Returns a new instance of Log.
5 6 7 8 9 10 |
# 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 |
Instance Method Details
#data ⇒ Object
36 37 38 |
# File 'lib/verkilo/log.rb', line 36 def data @data end |
#data=(h) ⇒ Object
31 32 33 34 35 |
# 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') ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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 ⇒ Object
46 47 48 |
# File 'lib/verkilo/log.rb', line 46 def filename File.join([@root_dir, '.verkilo', "#{@type}.yml"]) end |
#total!(k = nil) ⇒ Object
25 26 27 28 29 30 |
# 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 ⇒ Object
39 40 41 42 43 44 45 |
# 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 |