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
} + @@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

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



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

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

#-(other) ⇒ Object



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

def -        other ; r2n(ruby_date - (is_date?(other) ? other.ruby_date : other))          ; end

#<(other) ⇒ Object



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

def <   other ; is_date?(other) && ruby_date < other.ruby_date   ; end

#<=>(other) ⇒ Object



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

def <=> other ; is_date?(other) && ruby_date <=> other.ruby_date ; end

#==(other) ⇒ Object



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

def ==  other ; is_date?(other) && ruby_date == other.ruby_date  ; end

#>(other) ⇒ Object



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

def >   other ; is_date?(other) && ruby_date > other.ruby_date   ; end

#_nydp_get(key) ⇒ Object



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

def _nydp_get             key ; lookup key, ruby_date                                                    ; end

#_nydp_keysObject



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

def _nydp_keys                ; @@keys.to_a                                                              ; end

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

args not used



68
69
70
71
72
73
# File 'lib/nydp/date.rb', line 68

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



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

def beginning_of_month y, m, d, w ; build(y, m, 1)               ; end

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



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

def beginning_of_week  y, m, d, w ; ruby_date - ((w - 1) % 7)    ; end

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



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

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



84
85
86
87
88
89
90
# File 'lib/nydp/date.rb', line 84

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



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

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



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

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



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

def end_of_week        y, m, d, w ; ruby_date + ((7 - w) % 7)    ; end

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



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

def end_of_year        y, m, d, w ; build(y, 12, 31)             ; end

#eql?(d) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?    d ; self == d                                        ; end

#hashObject



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

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)


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

def is_date? other ; other.is_a? Nydp::Date                                                ; end

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



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

def last_month         y, m, d, w ; ruby_date.prev_month         ; end

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



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

def last_week          y, m, d, w ; ruby_date - 7                ; end

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



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

def last_year          y, m, d, w ; ruby_date.prev_year          ; end

#lookup(key, date) ⇒ Object



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

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



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

def next_month         y, m, d, w ; ruby_date.next_month         ; end

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



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

def next_week          y, m, d, w ; ruby_date + 7                ; end

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



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

def next_year          y, m, d, w ; ruby_date.next_year          ; end

#nydp_typeObject



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

def nydp_type ; :date                                            ; end

#splat(date) ⇒ Object



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

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



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

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



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

def yesterday          y, m, d, w ; ruby_date - 1                ; end