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: File.dirname(filename), config: {}) ⇒ 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/weekly_planner.rb', line 23

def initialize(filename='weekly-planner.txt', 
               path: File.dirname(filename), config: {})
  
  @filename, @path = File.basename(filename), File.expand_path(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]
      
    end
  
  else      
    @dx = new_dx
  end
  
  # check for monthly-planner entries which 
  #   should be added to the weekly-planner
  monthlyplanner_filepath = config[:monthlyplanner_filepath]    
  
  if monthlyplanner_filepath then

    @mp = mp = MonthlyPlanner.new monthlyplanner_filepath
    
    mp.this_week.each do |x|

      i = x.date.day - Date.today.day
      a = @dx.all[i].x.lines
      s = "%s %s" % [x.time, x.desc]

      @dx.all[i].x =  (a << s).join("\n") if a.grep(s).empty?

    end      
    
  end
  
  
end

Instance Attribute Details

#mpObject (readonly)

Returns the value of attribute mp.



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

def mp
  @mp
end

#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



87
88
89
# File 'lib/weekly_planner.rb', line 87

def dx()
  @dx
end

#format_row(heading, content) ⇒ Object



194
195
196
197
# File 'lib/weekly_planner.rb', line 194

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

#ordinal(n) ⇒ Object



199
200
201
202
# File 'lib/weekly_planner.rb', line 199

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



91
92
93
94
95
96
97
98
99
# File 'lib/weekly_planner.rb', line 91

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