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

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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



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

def dx()
  @dx
end

#find_by_wday(wday) ⇒ Object



92
93
94
# File 'lib/weekly_planner.rb', line 92

def find_by_wday(wday)
  @dx.all.detect {|x| Date.parse(x.id).wday == wday }
end

#format_row(heading, content) ⇒ Object



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

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

#ordinal(n) ⇒ Object



204
205
206
207
# File 'lib/weekly_planner.rb', line 204

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



96
97
98
99
100
101
102
103
104
# File 'lib/weekly_planner.rb', line 96

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