Class: Weekday

Inherits:
Date
  • Object
show all
Defined in:
lib/weekday.rb

Constant Summary collapse

OLDER =
'-'
NEWER =
'+'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Date

#to_month

Methods included from WeekDayCoreExt

#is_weekday?, #to_weekday, #to_weekday!

Class Method Details

.civil(y = -4712,, m = 1, d = 1, sg = ITALY) ⇒ Object



24
25
26
# File 'lib/weekday.rb', line 24

def self.civil(y=-4712, m=1, d=1, sg=ITALY)
  weekday_or_nearest(y,m,d,OLDER)
end

.civil!(y = -4712,, m = 1, d = 1, sg = ITALY) ⇒ Object



32
33
34
35
36
# File 'lib/weekday.rb', line 32

def self.civil!(y=-4712, m=1, d=1, sg=ITALY)
  day = new(y,m,d)
  raise "Is not a Weekday" unless day.is_weekday?
  day
end

.civil_or_newer(y = -4712,, m = 1, d = 1, sg = ITALY) ⇒ Object



28
29
30
# File 'lib/weekday.rb', line 28

def self.civil_or_newer(y=-4712, m=1, d=1, sg=ITALY)
  weekday_or_nearest(y,m,d,NEWER)
end

.todayObject



68
69
70
# File 'lib/weekday.rb', line 68

def self.today
  Date.today.to_weekday
end

.today!Object



72
73
74
# File 'lib/weekday.rb', line 72

def self.today!
  Date.today.to_weekday!
end

Instance Method Details

#+(x) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/weekday.rb', line 55

def +(x)
  if x.is_a?(Numeric)
    days = x % 5
    weeks = (x / 5) * 7
    days += 2 if (self.cwday + days) >= 6
    return self.class.new!(@ajd + weeks + days, @of, @sg)
  elsif x.is_a?(Date)
    return @ajd + x.ajd
  else
    raise TypeError, 'expected numeric or date'
  end
end

#-(x) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/weekday.rb', line 38

def -(x)
  if x.is_a?(Numeric)
#      weekend_offset = 0
#      weekend_offset += 2 if (self.cwday - (x % 5)) <= 0
#      weekend_offset += ((x / 5)) * 2 if x >= 5
#      return self.class.new!(@ajd - x - weekend_offset, @of, @sg)
    days = x % 5
    weeks = (x / 5) * 7
    days += 2 if (self.cwday - days) <= 0
    return self.class.new!(@ajd - weeks - days, @of, @sg)
  elsif x.is_a?(Date) 
    return @ajd - x.ajd
  else
    raise TypeError, 'expected numeric or date'
  end
end