-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringDate.rb
39 lines (36 loc) · 895 Bytes
/
StringDate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
require "date"
class StringDate
attr_reader :description,:date
@@PREDEFINED_DATES={"yesterday"=>lambda{Date.today-1}, "tomorrow"=>lambda{Date.today+1}, "today"=>lambda{Date.today}}
@@PREDEFINED_RANGES={"this_week"=>(Date.today..Date.today+7)}
def initialize(string)
if(@@PREDEFINED_DATES.has_key?(string))
@date=@@PREDEFINED_DATES[string].call
else
@date=Date.strptime(string, '%d/%m/%Y')
end
end
def to_s
if (@date== Date.today)
return "today"
elsif (@date==(Date.today+1))
return "tomorrow"
elsif (@date==(Date.today-1))
return "yesterday"
else
return @date.strftime('%d/%m/%Y')
end
end
def ==(other)
@date==other.date
end
def <=>(other)
@date<=>other.date
end
def is_in?(range)
if(@@PREDEFINED_RANGES.has_key?(range))
return @@PREDEFINED_RANGES[range] === @date
end
range===@date
end
end