Class: DailyPlanner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = 'daily-planner.txt', path: '.', now: DateTime.now.to_date, config: {}) ⇒ DailyPlanner

Returns a new instance of DailyPlanner.



14
15
16
17
18
19
20
21
22
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
# File 'lib/daily_planner.rb', line 14

def initialize(filename='daily-planner.txt', path: '.', 
                                       now: DateTime.now.to_date, config: {})
  
  @filename, @path, @now = filename, path, now
  
  fpath = File.join(path, filename)
  
  if File.exists?(fpath) then
    
    @rx = import_to_rx(File.read(fpath))
    @d = Date.parse(@rx.date)
    archive()
    
    # if the date isn't today then update it to today
    refresh() if @d != @now

  else      
    @rx = new_rx
  end
  
  # check for weekly-planner entries which 
  #   should be added to the daily-planner
  weeklyplanner_filepath = config[:weeklyplanner_filepath]
  
  if weeklyplanner_filepath then

    dx = Dynarex.new weeklyplanner_filepath
    record = dx.find_by_id (now+1).strftime("%Y%m%d")      

    items = record.x.lines[2..-1]
    @rx.tomorrow << "\n" + items.join if record and items

  end
  
end

Instance Attribute Details

#to_sObject (readonly)

Returns the value of attribute to_s.



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

def to_s
  @to_s
end

Instance Method Details

#format_row(heading, s) ⇒ Object



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

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

#ordinal(n) ⇒ Object



104
105
106
107
# File 'lib/daily_planner.rb', line 104

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

#rxObject



50
51
52
# File 'lib/daily_planner.rb', line 50

def rx()
  @rx
end

#save(filename = @filename) ⇒ Object



54
55
56
57
58
59
# File 'lib/daily_planner.rb', line 54

def save(filename=@filename)
  
  s = File.basename(filename) + "\n" + rx_to_s(@rx).lines[1..-1].join
  File.write File.join(@path, filename), s
      
end