Class: DocbookStatus::History

Inherits:
Object
  • Object
show all
Defined in:
lib/docbook_status/history.rb

Overview

Manages the history of writing progress in two modes. In session or demon mode the history shows progress for the user session. In normal mode the history is only maintained for calendar days, weeks, months.

The writing progress can (but must not) measured with these optional items:

  • start date (date of initialization)

  • scheduled end date

  • total word count goal

  • daily word count goal

  • file name

  • goal total

  • goal daily

  • start date

  • planned end date

current entries

  • timestamp

  • word count

archive entries

  • date

  • start

  • end

  • min

  • max

  • ctr (number of entries for the day)

Constant Summary collapse

HISTORY_FILE =

History file, YAML format

'dbs_work.yml'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, end_planned = nil, goal_total = 0, goal_daily = 0) ⇒ History

Load the exisiting writing history



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/docbook_status/history.rb', line 45

def initialize(name,end_planned=nil,goal_total=0,goal_daily=0)
  if File.exists?(HISTORY_FILE)
    @history = YAML.load_file(HISTORY_FILE)
  else
    @history = {:file => name,
      :goal => {
        :start => Date.today,
        :end => end_planned,
        :goal_total => goal_total,
        :goal_daily => goal_daily},
      :current => [],
      :archive => {}}
  end
end

Class Method Details

.exists?Boolean

Does the history file exist?

Returns:

  • (Boolean)


40
41
42
# File 'lib/docbook_status/history.rb', line 40

def self.exists?()
  File.exists?(HISTORY_FILE)
end

Instance Method Details

#add(ts, word_count) ⇒ Object

Add to the history



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/docbook_status/history.rb', line 73

def add(ts,word_count)
  # Ruby 1.8 doesn't have DateTime#to_date, so we check that here
  begin
    k = ts.to_date
  rescue NoMethodError
    k = Date.parse(ts.to_s)
  end
  unless (@history[:archive][k].nil?)
    @history[:archive][k][:min] = word_count if @history[:archive][k][:min] > word_count
    @history[:archive][k][:max] = word_count if @history[:archive][k][:max] < word_count
    @history[:archive][k][:end] = word_count
    @history[:archive][k][:ctr] += 1
  else
    @history[:archive][k] = {:min => word_count, :max => word_count, :start => word_count, :end => word_count, :ctr => 1}
  end
end

#daily_words(tw) ⇒ Object



68
69
70
# File 'lib/docbook_status/history.rb', line 68

def daily_words(tw)
  @history[:goal][:goal_daily]=tw
end

#goalsObject

Return the goals



101
102
103
# File 'lib/docbook_status/history.rb', line 101

def goals
  @history[:goal]
end

#history?Boolean

Is there already a history?

Returns:

  • (Boolean)


91
92
93
# File 'lib/docbook_status/history.rb', line 91

def history?
  @history[:archive].length != 0
end

#planned_end(date) ⇒ Object



60
61
62
# File 'lib/docbook_status/history.rb', line 60

def planned_end(date)
  @history[:goal][:end]=date
end

#saveObject

Save the writing history



106
107
108
# File 'lib/docbook_status/history.rb', line 106

def save
  File.open(HISTORY_FILE, 'w') {|f| YAML.dump(@history,f)}
end

#todayObject

Convenience - returns the statistics for today



96
97
98
# File 'lib/docbook_status/history.rb', line 96

def today
  @history[:archive][Date.today]
end

#total_words(tw) ⇒ Object



64
65
66
# File 'lib/docbook_status/history.rb', line 64

def total_words(tw)
  @history[:goal][:goal_total]=tw
end