Class: Nydp::Date
Constant Summary
collapse
- MONTH_SIZES =
[nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- @@pass_through =
%i{ monday? tuesday? wednesday? thursday? friday? saturday? sunday? }
- @@keys =
Set.new %i{
year month week_day day
last_year next_year beginning_of_year end_of_year
last_month next_month beginning_of_month end_of_month
last_week next_week beginning_of_week end_of_week
yesterday tomorrow age past?
future?
} + @@pass_through
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#+(int) ⇒ Object
-
#-(other) ⇒ Object
-
#<(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
-
#>(other) ⇒ Object
-
#_nydp_get(key) ⇒ Object
-
#_nydp_keys ⇒ Object
-
#age(y, m, d, w) ⇒ Object
-
#beginning_of_month(y, m, d, w) ⇒ Object
-
#beginning_of_week(y, m, d, w) ⇒ Object
-
#beginning_of_year(y, m, d, w) ⇒ Object
-
#build(y, m, d) ⇒ Object
-
#change(amount, attr) ⇒ Object
-
#day(y, m, d, w) ⇒ Object
-
#dispatch(key, y, m, d, w) ⇒ Object
-
#end_of_month(y, m, d, w) ⇒ Object
-
#end_of_week(y, m, d, w) ⇒ Object
-
#end_of_year(y, m, d, w) ⇒ Object
-
#eql?(d) ⇒ Boolean
-
#future?(*_) ⇒ Boolean
-
#hash ⇒ Object
-
#initialize(ruby_date) ⇒ Date
constructor
-
#inspect ⇒ Object
-
#is_date?(other) ⇒ Boolean
-
#last_month(y, m, d, w) ⇒ Object
-
#last_week(y, m, d, w) ⇒ Object
-
#last_year(y, m, d, w) ⇒ Object
-
#lookup(key, date) ⇒ Object
-
#month(y, m, d, w) ⇒ Object
-
#next_month(y, m, d, w) ⇒ Object
-
#next_week(y, m, d, w) ⇒ Object
-
#next_year(y, m, d, w) ⇒ Object
-
#past?(*_) ⇒ Boolean
-
#splat(date) ⇒ Object
-
#to_date ⇒ Object
-
#to_ruby ⇒ Object
-
#to_s ⇒ Object
-
#tomorrow(y, m, d, w) ⇒ Object
-
#week_day(y, m, d, w) ⇒ Object
-
#year(y, m, d, w) ⇒ Object
-
#yesterday(y, m, d, w) ⇒ Object
Methods included from Helper
#cons, #list, #literal?, #pair?, #sig, #sym, #sym?
Methods included from Converter
#n2r, #r2n, #rubify
Constructor Details
#initialize(ruby_date) ⇒ Date
Returns a new instance of Date.
19
|
# File 'lib/nydp/date.rb', line 19
def initialize ruby_date ; @ruby_date = ruby_date ; end
|
Instance Attribute Details
#ruby_date ⇒ Object
Returns the value of attribute ruby_date.
7
8
9
|
# File 'lib/nydp/date.rb', line 7
def ruby_date
@ruby_date
end
|
Instance Method Details
33
|
# File 'lib/nydp/date.rb', line 33
def + int ; int.is_a?(Integer) ? r2n(ruby_date + int) : r2n(change(*int.to_ruby)) ; end
|
32
|
# File 'lib/nydp/date.rb', line 32
def - other ; ruby_date - other ; end
|
26
|
# File 'lib/nydp/date.rb', line 26
def < other ; is_date?(other) && ruby_date < other ; end
|
#<=>(other) ⇒ Object
28
|
# File 'lib/nydp/date.rb', line 28
def <=> other ; is_date?(other) && ruby_date <=> other ; end
|
#==(other) ⇒ Object
27
|
# File 'lib/nydp/date.rb', line 27
def == other ; is_date?(other) && ruby_date == other ; end
|
25
|
# File 'lib/nydp/date.rb', line 25
def > other ; is_date?(other) && ruby_date > other ; end
|
#_nydp_get(key) ⇒ Object
86
|
# File 'lib/nydp/date.rb', line 86
def _nydp_get key ; lookup key, ruby_date ; end
|
#_nydp_keys ⇒ Object
82
|
# File 'lib/nydp/date.rb', line 82
def _nydp_keys ; @@keys.to_a ; end
|
#age(y, m, d, w) ⇒ Object
71
72
73
74
75
76
|
# File 'lib/nydp/date.rb', line 71
def age y,m,d,w interval = (::Date.today - ruby_date) / 365.0
age_in_years = interval.to_i
= (12 * (interval - age_in_years)).to_i
{ years: age_in_years, months: }
end
|
#beginning_of_month(y, m, d, w) ⇒ Object
60
|
# File 'lib/nydp/date.rb', line 60
def beginning_of_month y, m, d, w ; build(y, m, 1) ; end
|
#beginning_of_week(y, m, d, w) ⇒ Object
65
|
# File 'lib/nydp/date.rb', line 65
def beginning_of_week y, m, d, w ; ruby_date - ((w - 1) % 7) ; end
|
#beginning_of_year(y, m, d, w) ⇒ Object
55
|
# File 'lib/nydp/date.rb', line 55
def beginning_of_year y, m, d, w ; build(y, 1, 1) ; end
|
#build(y, m, d) ⇒ Object
10
11
12
13
14
15
16
17
|
# File 'lib/nydp/date.rb', line 10
def build y, m, d
if m < 1
build( y - 1, m + 12, d )
else
s = MONTH_SIZES[m]
::Date.new(y, m, (d > s ? s : d))
end
end
|
#change(amount, attr) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/nydp/date.rb', line 87
def change amount, attr
if attr == :day ; (ruby_date + amount)
elsif attr == :week ; (ruby_date + (7 * amount))
elsif attr == :month ; (ruby_date >> amount)
elsif attr == :year ; (ruby_date >> (12 * amount))
end
end
|
#day(y, m, d, w) ⇒ Object
47
|
# File 'lib/nydp/date.rb', line 47
def day y, m, d, w ; d ; end
|
#dispatch(key, y, m, d, w) ⇒ Object
83
|
# File 'lib/nydp/date.rb', line 83
def dispatch key, y, m, d, w ; self.send(key, y, m, d, w) if _nydp_keys.include?(key) ; end
|
#end_of_month(y, m, d, w) ⇒ Object
61
|
# File 'lib/nydp/date.rb', line 61
def end_of_month y, m, d, w ; beginning_of_month(*splat(ruby_date.next_month)) - 1 ; end
|
#end_of_week(y, m, d, w) ⇒ Object
66
|
# File 'lib/nydp/date.rb', line 66
def end_of_week y, m, d, w ; ruby_date + ((7 - w) % 7) ; end
|
#end_of_year(y, m, d, w) ⇒ Object
56
|
# File 'lib/nydp/date.rb', line 56
def end_of_year y, m, d, w ; build(y, 12, 31) ; end
|
#eql?(d) ⇒ Boolean
29
|
# File 'lib/nydp/date.rb', line 29
def eql? d ; self == d ; end
|
#future?(*_) ⇒ Boolean
50
|
# File 'lib/nydp/date.rb', line 50
def future? *_ ; ruby_date > ::Date.today ; end
|
30
|
# File 'lib/nydp/date.rb', line 30
def hash ; ruby_date.hash ; end
|
24
|
# File 'lib/nydp/date.rb', line 24
def inspect ; ruby_date.inspect ; end
|
#is_date?(other) ⇒ Boolean
31
|
# File 'lib/nydp/date.rb', line 31
def is_date? other ; other.is_a? ::Date ; end
|
#last_month(y, m, d, w) ⇒ Object
58
|
# File 'lib/nydp/date.rb', line 58
def last_month y, m, d, w ; ruby_date.prev_month ; end
|
#last_week(y, m, d, w) ⇒ Object
63
|
# File 'lib/nydp/date.rb', line 63
def last_week y, m, d, w ; ruby_date - 7 ; end
|
#last_year(y, m, d, w) ⇒ Object
53
|
# File 'lib/nydp/date.rb', line 53
def last_year y, m, d, w ; ruby_date.prev_year ; end
|
#lookup(key, date) ⇒ Object
85
|
# File 'lib/nydp/date.rb', line 85
def lookup key, date ; r2n(dispatch(key.to_s.gsub(/-/, '_').to_sym, *splat(date))) ; end
|
#month(y, m, d, w) ⇒ Object
46
|
# File 'lib/nydp/date.rb', line 46
def month y, m, d, w ; m ; end
|
#next_month(y, m, d, w) ⇒ Object
59
|
# File 'lib/nydp/date.rb', line 59
def next_month y, m, d, w ; ruby_date.next_month ; end
|
#next_week(y, m, d, w) ⇒ Object
64
|
# File 'lib/nydp/date.rb', line 64
def next_week y, m, d, w ; ruby_date + 7 ; end
|
#next_year(y, m, d, w) ⇒ Object
54
|
# File 'lib/nydp/date.rb', line 54
def next_year y, m, d, w ; ruby_date.next_year ; end
|
#past?(*_) ⇒ Boolean
51
|
# File 'lib/nydp/date.rb', line 51
def past? *_ ; ruby_date < ::Date.today ; end
|
#splat(date) ⇒ Object
84
|
# File 'lib/nydp/date.rb', line 84
def splat date ; [date.year, date.month, date.day, date.wday] ; end
|
21
|
# File 'lib/nydp/date.rb', line 21
def to_date ; ruby_date ; end
|
23
|
# File 'lib/nydp/date.rb', line 23
def to_ruby ; ruby_date ; end
|
22
|
# File 'lib/nydp/date.rb', line 22
def to_s ; ruby_date.to_s ; end
|
#tomorrow(y, m, d, w) ⇒ Object
69
|
# File 'lib/nydp/date.rb', line 69
def tomorrow y, m, d, w ; ruby_date + 1 ; end
|
#week_day(y, m, d, w) ⇒ Object
48
|
# File 'lib/nydp/date.rb', line 48
def week_day y, m, d, w ; w ; end
|
#year(y, m, d, w) ⇒ Object
45
|
# File 'lib/nydp/date.rb', line 45
def year y, m, d, w ; y ; end
|
#yesterday(y, m, d, w) ⇒ Object
68
|
# File 'lib/nydp/date.rb', line 68
def yesterday y, m, d, w ; ruby_date - 1 ; end
|