class Verku::Stats

Attributes

root_dir[R]

Public Class Methods

new(root_dir) click to toggle source
# File lib/verku/stats.rb, line 14
def initialize(root_dir)
  @root_dir = root_dir
  @files = Dir["#{root_dir}/text/**/[0-9]*.{md,markdown,mkd}"]
  @words = 0
  @progress = (File.exist?(log)) ? JSON.parse(File.open(log,'r').read).clone : {}
end

Public Instance Methods

lasttime() click to toggle source
# File lib/verku/stats.rb, line 55
def lasttime
  p = nil
  @progress.keys.sort.each do |k|
    break if k == now
    p = k
  end
  p
end
log() click to toggle source
# File lib/verku/stats.rb, line 21
def log
  "#{root_dir}/.kalkulado"
end
now() click to toggle source
# File lib/verku/stats.rb, line 28
def now
  Date.today.to_s
end
remaining() click to toggle source
# File lib/verku/stats.rb, line 73
def remaining
  target - words
end
target() click to toggle source
# File lib/verku/stats.rb, line 25
def target
  Verku.config(@root_dir)['wordcount'] || 1000
end
text() click to toggle source
# File lib/verku/stats.rb, line 69
def text
  @text = nil
  @text = @files.map{|f| File.open(f,'r').read}.join("\n\n\n") if @text.nil?
end
today() click to toggle source
# File lib/verku/stats.rb, line 66
def today
  @words - @progress[lasttime]
end
words() click to toggle source
# File lib/verku/stats.rb, line 31
def words
  if @words == 0
    File.open(log,'w').write( JSON.generate(Hash.new) ) unless File.exist?(log)

    most_recent = @files.max_by {|f| File.mtime(f)}
    if !@progress[now].nil? and File.mtime(log) > File.mtime(most_recent)
      @progress[now]
    else
      @progress[now] = text.split(/\s/).keep_if { |word| word.length > 0 }.count
      @progress[Date.yesterday.to_s] = @progress[now] if @progress.keys.count == 1
    end
  end
  if (lasttime != Date.yesterday.to_s)
    # Update the record.
    n = lasttime
    w = @progress[lasttime]
    n.upto(Date.yesterday.to_s) do |k|
      puts "Upto #{k}: @progress[#{k}] = #{w}"
      @progress[k] = w
    end
  end
  File.open(log,'w').write( JSON.generate(@progress) )
  @words = @progress[now]
end
yesterday() click to toggle source
# File lib/verku/stats.rb, line 63
def yesterday
  @progress[lasttime]
end