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.



14
15
16
17
18
19
20
21
22
# File 'lib/weekly_planner.rb', line 14

def initialize(filename='weekly-planner.txt', path: '.')
  
  @filename, @path = filename, path
  
  fpath = File.join(path, filename)
  @s = File.exists?(fpath) ? File.read(fpath) :  create()    
  @dx = create_dx(@s)
  
end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



12
13
14
# File 'lib/weekly_planner.rb', line 12

def to_s
  @to_s
end

Instance Method Details

#dxObject



24
25
26
# File 'lib/weekly_planner.rb', line 24

def dx()
  @dx
end

#ordinal(n) ⇒ Object



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

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



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
# File 'lib/weekly_planner.rb', line 28

def save(filename=@filename)
  
  s = File.basename(filename) + "\n" + @s.lines[1..-1].join
  File.write File.join(@path, filename), s
  
  # archive the weekly planner
  # e.g. weeklyplanner/2015/wp50.xml
  d = Date.strptime(@dx.all.first.id, "%Y%m%d")
  archive_path = File.join(@path, d.year.to_s)
  FileUtils.mkdir_p archive_path
  
  a = @dx.all.partition {|x| Date.strptime(x.id, "%Y%m%d").cweek == d.cweek }

  a.each do |rows|
    
    filename = "wp%s.xml" % Date.strptime(rows.first.id, "%Y%m%d").cweek
    filepath = File.join(archive_path, filename)
    
    if File.exists? filepath then
      
      dx = Dynarex.new filepath
      
      rows.each do |row|
        
        record = dx.find_by_id row.id
        
        if record then
          record.x = row.x unless record.x == row.x
        else
          dx.create row
        end
      end
      
      dx.save filepath
      
    else
      
      dx = Dynarex.new 'sections[title]/section(x)'
      rows.each {|row| dx.create row }
      dx.save filepath        
       
    end
          
  end
  
end