Class: WeeklyPlanner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'weekly-planner.txt', path: '.') ⇒ WeeklyPlanner

Returns a new instance of WeeklyPlanner.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/weekly_planner.rb', line 23

def initialize(filename='weekly-planner.txt', path: '.')
  
  @filename, @path = filename, path
  
  fpath = File.join(path, filename)
  
  if File.exists?(fpath) then

    dx = import_to_dx(File.read(fpath))
    @dx = refresh File.join(path, filename.sub(/\.txt$/,'.xml')), dx
    sync_archive()         
    # purge any past dates
    while @dx.all.first and Date.parse(@dx.all.first.id, "%Y%m%d") != DateTime.now.to_date \
                                                    and @dx.all.length > 0 do
      @dx.all.first.delete
      
    end
    
    # new days to add?
    len = 7 - @dx.all.length
    
    if @dx.all.length > 0 and len > 0 then
      
      date = Date.strptime(@dx.all.last.id, "%Y%m%d") + 1
      
      len.times.with_index do |row, i|
        @dx.create({x: (date + i).strftime("# %d-%b-%Y\n\n")}, \
                                           id: (date + i).strftime("%Y%m%d"))
      end
      
      sync_archive @dx.all[-(len)..-1]
      
    else
      
      @dx = new_dx
      
    end
  
  else      
    @dx = new_dx
  end
  
end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



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

def to_s
  @to_s
end

Instance Method Details

#dxObject



67
68
69
# File 'lib/weekly_planner.rb', line 67

def dx()
  @dx
end

#format_row(heading, content) ⇒ Object



174
175
176
177
# File 'lib/weekly_planner.rb', line 174

def format_row(heading, content)
  content.prepend "\n\n" unless content.empty?
  "%s\n%s%s" % [heading, '-' * heading.length, content]
end

#ordinal(n) ⇒ Object



179
180
181
182
# File 'lib/weekly_planner.rb', line 179

def ordinal(n)
  n.to_s + ( (10...20).include?(n) ? 'th' : \
                  %w{ th st nd rd th th th th th th }[n % 10] )
end

#save(filename = @filename) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/weekly_planner.rb', line 71

def save(filename=@filename)
# 
  s = File.basename(filename) + "\n" + dx_to_s(@dx).lines[1..-1].join
  File.write File.join(@path, filename), s
  @dx.save File.join(@path, filename.sub(/\.txt$/,'.xml'))
  
  sync_archive()
  
end