Class: Weekday
- Inherits:
-
Object
- Object
- Weekday
- 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
Class Method Summary collapse
-
.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.
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
36 37 38 39 40 41 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 |
# File 'lib/weekday.rb', line 36 def self.method_missing(m, *args) # 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 |