Class: Verku::Stats

Inherits:
Object
  • Object
show all
Defined in:
lib/verku/stats.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_dir) ⇒ Stats

Returns a new instance of Stats.


14
15
16
17
18
19
# 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

Instance Attribute Details

#root_dirObject (readonly)

Returns the value of attribute root_dir


12
13
14
# File 'lib/verku/stats.rb', line 12

def root_dir
  @root_dir
end

Instance Method Details

#lasttimeObject


55
56
57
58
59
60
61
62
# 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

#logObject


21
22
23
# File 'lib/verku/stats.rb', line 21

def log
  "#{root_dir}/.kalkulado"
end

#nowObject


28
29
30
# File 'lib/verku/stats.rb', line 28

def now
  Date.today.to_s
end

#remainingObject


73
74
75
# File 'lib/verku/stats.rb', line 73

def remaining
  target - words
end

#targetObject


25
26
27
# File 'lib/verku/stats.rb', line 25

def target
  Verku.config(@root_dir)['wordcount'] || 1000
end

#textObject


69
70
71
72
# 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

#todayObject


66
67
68
# File 'lib/verku/stats.rb', line 66

def today
  @words - @progress[lasttime] 
end

#wordsObject


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 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

#yesterdayObject


63
64
65
# File 'lib/verku/stats.rb', line 63

def yesterday
  @progress[lasttime]
end