Class: Weekday

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

Constant Summary collapse

COUNTWORDS =
%w(first second third fourth fifth last)
DAYNAMES =
%w(sunday monday tuesday wednesday thursday friday saturday)
DAYNAMESS =
%w(sundays mondays tuesdays wednesdays thursdays fridays saturdays)
PREV =
%w(nil nil) + COUNTWORDS
ILLEGAL_ARGUMENTS =
"illegal month or year number"
DAYS_PER_WEEK =
7
NEXT =
'next'
PREVIOUS =
'previous'

Class Method Summary collapse

Class Method Details

.method_missing(m, *args) ⇒ Object

Weekday.method_missing implements the methods like

  • Weekday.second_tuesday(2010,12) or

  • Weekday.last_monday(2010,11)

The return value is a date object or nil if there is no Nth Xday in this month

  • Weekday.mondays(2011,2) returns an array of all mondays in this month

  • Weekday.first_monday(2011) returns the very first monday in this year

  • Weekday.last_monday(2011) returns the very last monday in this year

  • Weekday.mondays(2011) returns all mondays in this year as an array of Date objects

  • Weekday.first_thursdays(2011) returns all first thursdays in this year as an array of Date objects

  • Weekday.next_first_tuesday returns the next specified date (today if this date matches today). This method can take a argument of type Date - if no argument is given, this argument defaults to the current date.

  • Weekday.previous_first_tuesday returns the previous specified date (today if this date matches today). This method can take a argument of type Date - if no argument is given, this argument defaults to the current date.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/weekday.rb', line 42

def self.method_missing(m, *args)
  count_s,weekday = m.to_s.split('_')
  # next_first_thursday
  if m.to_s.split('_').first == NEXT
    n,count_s,weekday = m.to_s.split('_')
    today = args[0]
    today ||= Date.today
    month = today.month
    year = today.year
    nn = nil
    loop do
      nn = self.send(count_s+'_'+weekday,year,month)
      break if nn
      year,month = next_month(year,month)
    end
    if nn < today
      loop do
        nn = self.send(count_s+'_'+weekday,next_month(year,month)[0],
          next_month(year,month)[1])
        break if nn
        year,month = next_month(year,month)
      end
    end
    return nn
  end
  # end next_first_thursday
  
  # previous_first_thursday
  if m.to_s.split('_').first == PREVIOUS
    n,count_s,weekday = m.to_s.split('_')
    today = args[0]
    today ||= Date.today
    month = today.month
    year = today.year
    nn = nil
    loop do
      nn = self.send(count_s+'_'+weekday,year,month)
      break if nn
      year,month = previous_month(year,month)
    end
    if nn > today
      loop do
        nn = self.send(count_s+'_'+weekday, previous_month(year,month)[0],
          previous_month(year,month)[1])
        break if nn
        year,month = previous_month(year,month)
      end
    end
    return nn
  end
  # end previous_first_thursday

  # first_thursdays(year)
  if DAYNAMESS.include?(weekday) and args.size == 1
    unless COUNTWORDS.include?(count_s)
      raise NoMethodError, "undefined method `#{m}'"
    end
    count = COUNTWORDS.index(count_s)+1
    wnum = DAYNAMESS.index(weekday)
    year = args[0]
    result = []
    1.upto(12) do |month|
      result << self.send(count_s+"_"+DAYNAMES[wnum],year,month)
    end
    return result
  end
  # end first_thursdays

  # self.mondays .. self.sundays
  if DAYNAMESS.include?(m.to_s)
    wnum = DAYNAMESS.index(m.to_s)
    raise ArgumentError, ILLEGAL_ARGUMENTS unless args[0] or args[1]
    # mondays .. sundays without a month => calculate complete year
    unless args[1]
      result = []
      1.upto(12) do |mm|
        result = (result + self.send(m,args[0],mm)).flatten
      end
      return result
    end
    begin
      d = Date.new(args[0],args[1])
    rescue ArgumentError
      raise ArgumentError, ILLEGAL_ARGUMENTS
    end
    result = []
    while d.month == args[1] do
      if d.wday == wnum
        result << d
        loop do
          d += DAYS_PER_WEEK
          break if not d.month == args[1]
          result << d
        end
      end
      d += 1
    end
    return result
  end
  count,weekday = m.to_s.split('_')
  unless COUNTWORDS.include?(count)
    raise NoMethodError, "undefined method `#{m}'"
  end
  count = COUNTWORDS.index(count)+1
  wnum = DAYNAMES.index(weekday)
  unless wnum
    raise NoMethodError, "undefined method `#{m}'"
  end
  # self.last_monday .. self.last_sunday
  if count == COUNTWORDS.index(COUNTWORDS[-1])+1
    args[1] = 12 if args[1] == nil # december if no month was given at
    # last_xxx(year)
    d = Date.new(args[0],args[1],-1)
    while not d.wday == wnum do
      d -= 1
    end
    return d
  end
  case count
  when 1
    # at least a year must be given
    raise ArgumentError, ILLEGAL_ARGUMENTS unless args[0] or args[1]
    args[1] = 1 if args[1] == nil # january if no month was given at
    # first_xxx(year)
    begin
      d = Date.new(args[0],args[1],1)
    rescue ArgumentError
      raise ArgumentError, ILLEGAL_ARGUMENTS
    end
    while not d.wday == wnum do
      d += 1
    end
    return d
  when 2..5
    m_name = (PREV[count] + '_' + weekday).to_sym
    prev = self.send((PREV[count] + '_' + weekday), args[0], args[1])
    result = if prev
      prev + DAYS_PER_WEEK
    else
      nil
    end
    unless result and result.month == args[1]
      result = nil
    end
    return result
  else
    raise NoMethodError, "undefined method `#{m}'"
  end
end

.nth(count, weekday, year, month) ⇒ Object

just another notation if you have to calculate the weekdays programmatically

  • count: 1..5 first, second; -1 last

  • weekday: 0: sunday, 1: monday, … 6: saturday

  • year

  • month

Raises:

  • (ArgumentError)


197
198
199
200
201
202
203
204
205
# File 'lib/weekday.rb', line 197

def Weekday.nth(count, weekday, year, month)
  raise ArgumentError unless count >= 1 and count <=5 or count == -1
  if count == -1
    s = COUNTWORDS.last + '_' + DAYNAMES[weekday]
  else
    s = COUNTWORDS[count-1] + '_' + DAYNAMES[weekday]
  end
  self.send(s,year,month)
end