Class: Nydp::Date

Inherits:
Object show all
Includes:
Helper
Defined in:
lib/nydp/date.rb

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

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_dateObject

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

#+(int) ⇒ Object



33
# File 'lib/nydp/date.rb', line 33

def +          int ; int.is_a?(Integer) ? r2n(ruby_date + int) : r2n(change(*int.to_ruby)) ; end

#-(other) ⇒ Object



32
# File 'lib/nydp/date.rb', line 32

def -        other ; ruby_date - other                      ; end

#<(other) ⇒ Object



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

#>(other) ⇒ Object



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_keysObject



82
# File 'lib/nydp/date.rb', line 82

def _nydp_keys                ; @@keys.to_a                                                 ; end

#age(y, m, d, w) ⇒ Object

args not used



71
72
73
74
75
76
# File 'lib/nydp/date.rb', line 71

def age y,m,d,w # args not used
  interval = (::Date.today - ruby_date) / 365.0
  age_in_years = interval.to_i
  extra_months = (12 * (interval - age_in_years)).to_i
  { years: age_in_years, months: extra_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

Returns:

  • (Boolean)


29
# File 'lib/nydp/date.rb', line 29

def eql?         d ; self == d                              ; end

#future?(*_) ⇒ Boolean

Returns:

  • (Boolean)


50
# File 'lib/nydp/date.rb', line 50

def future?                    *_ ; ruby_date > ::Date.today     ; end

#hashObject



30
# File 'lib/nydp/date.rb', line 30

def hash           ; ruby_date.hash                         ; end

#inspectObject



24
# File 'lib/nydp/date.rb', line 24

def inspect        ; ruby_date.inspect                      ; end

#is_date?(other) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

#to_dateObject



21
# File 'lib/nydp/date.rb', line 21

def to_date        ; ruby_date                              ; end

#to_rubyObject



23
# File 'lib/nydp/date.rb', line 23

def to_ruby        ; ruby_date                              ; end

#to_sObject



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