Class: Verku::Stats
- Inherits:
-
Object
- Object
- Verku::Stats
- Defined in:
- lib/verku/stats.rb
Instance Attribute Summary collapse
-
#root_dir ⇒ Object
readonly
Returns the value of attribute root_dir.
Instance Method Summary collapse
-
#initialize(root_dir) ⇒ Stats
constructor
A new instance of Stats.
- #lasttime ⇒ Object
- #log ⇒ Object
- #now ⇒ Object
- #remaining ⇒ Object
- #target ⇒ Object
- #text ⇒ Object
- #today ⇒ Object
- #words ⇒ Object
- #yesterday ⇒ Object
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_dir ⇒ Object (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
#lasttime ⇒ Object
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 |
#log ⇒ Object
21 22 23 |
# File 'lib/verku/stats.rb', line 21 def log "#{root_dir}/.kalkulado" end |
#now ⇒ Object
28 29 30 |
# File 'lib/verku/stats.rb', line 28 def now Date.today.to_s end |
#remaining ⇒ Object
73 74 75 |
# File 'lib/verku/stats.rb', line 73 def remaining target - words end |
#target ⇒ Object
25 26 27 |
# File 'lib/verku/stats.rb', line 25 def target Verku.config(@root_dir)['wordcount'] || 1000 end |
#text ⇒ Object
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 |
#today ⇒ Object
66 67 68 |
# File 'lib/verku/stats.rb', line 66 def today @words - @progress[lasttime] end |
#words ⇒ Object
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 |
#yesterday ⇒ Object
63 64 65 |
# File 'lib/verku/stats.rb', line 63 def yesterday @progress[lasttime] end |