Class: RemindersTxt

Inherits:
Object
  • Object
show all
Includes:
RXFHelperModule
Defined in:
lib/reminders_txt.rb

Direct Known Subclasses

RemindersTxtVoice

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_s = 'reminders.txt', now: Time.now, debug: false) ⇒ RemindersTxt

Returns a new instance of RemindersTxt.



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/reminders_txt.rb', line 24

def initialize(raw_s='reminders.txt', now: Time.now, debug: false)

  super()

  @now, @debug = now, debug
  
  puts ('@now: ' + @now.inspect).debug if @debug

  
  @filepath = raw_s
  
  if raw_s.lines.length > 1 then

    if raw_s.lstrip[0] == '<' then

      @filepath = 'reminders.xml'
      @dx = Dynarex.new raw_s
      
    else

      @filepath = File.join(Dir.pwd, 'reminders.txt')
      @dxfilepath = @filepath.sub(/.txt$/,'.xml')              

      @dx = Dynarex.new
      import_txt(raw_s)       
      
    end
    
  elsif File.extname(@filepath) == '.txt'

    s = FileX.read @filepath
    @filename =  File.basename(@filepath)
    @dxfilepath = @filepath.sub(/.txt$/,'.xml')      
    
    import_txt(s)
    
  else
    
    @dx = Dynarex.new @filepath
    
  end
end

Instance Attribute Details

#dxObject (readonly)

Returns the value of attribute dx.



22
23
24
# File 'lib/reminders_txt.rb', line 22

def dx
  @dx
end

#remindersObject (readonly)

Returns the value of attribute reminders.



22
23
24
# File 'lib/reminders_txt.rb', line 22

def reminders
  @reminders
end

Instance Method Details

#add(s) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/reminders_txt.rb', line 67

def add(s)

  s.strip!
  r = EventNlp.new(@now, params: {input: s}).parse(s)
  return if r.nil?
  
  @reminders << r
  refresh()        

end

#after(d) ⇒ Object



78
79
80
81
82
83
# File 'lib/reminders_txt.rb', line 78

def after(d)
  
  date = d.is_a?(String) ? Chronic.parse(d).to_datetime : d
  @dx.filter {|x| DateTime.parse(x.date) > date}
  
end

#before(d) ⇒ Object



85
86
87
88
89
90
# File 'lib/reminders_txt.rb', line 85

def before(d)
  
  future_date = d.is_a?(String) ? Chronic.parse(d).to_datetime : d
  @dx.filter {|x| DateTime.parse(x.date) < future_date}
  
end

#find(s) ⇒ Object



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

def find(s)
  @dx.filter {|x| x.title =~ /#{s}/i}
end

#this_weekObject Also known as: weekahead



119
120
121
# File 'lib/reminders_txt.rb', line 119

def this_week()
  upcoming days: 6
end

#this_yearObject



125
126
127
# File 'lib/reminders_txt.rb', line 125

def this_year()
  upcoming months: 12
end

#to_sObject



129
130
131
132
133
134
# File 'lib/reminders_txt.rb', line 129

def to_s()

  filename = File.basename(@filepath).sub(/\.xml$/, '.txt')
  [filename,  '=' * filename.length, '', *@dx.all.map(&:input)].join("\n")

end

#to_xmlObject



136
137
138
# File 'lib/reminders_txt.rb', line 136

def to_xml()
  @dx.to_xml pretty: true
end

#todayObject



111
112
113
# File 'lib/reminders_txt.rb', line 111

def today()
  upcoming 0
end

#tomorrowObject



115
116
117
# File 'lib/reminders_txt.rb', line 115

def tomorrow()
  upcoming days: 1
end

#upcoming(ndays = 5, days: ndays, months: nil) ⇒ Object



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

def upcoming(ndays=5, days: ndays, months: nil)
  
  next_date = if months then
    @now.to_datetime >> months.to_i
  else
    ((@now.to_date + days.to_i + 1).to_time - 1).to_datetime
  end
  
  @dx.filter {|x| DateTime.parse(x.date) <= next_date}
end

#updated?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/reminders_txt.rb', line 107

def updated?()
  @updated
end