meetup.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Meetup
  DAYS = [
    :monday,
    :tuesday,
    :wednesday,
    :thursday,
    :friday,
    :saturday,
    :sunday,
  ]

  INDICES = {
      :first => 0,
      :second => 1,
      :third => 2,
      :fourth => 3,
      :last => -1,
    }

  def initialize(month, year)
    @month = month
    @year = year
  end

  def day(weekday, schedule)
    ## Look up the day name
    weekday_num = DAYS.index(weekday)

    ## Find out information about the specified month
    first_weekday = Date.new(@year, @month, 1).wday - 1
    days_in_month = (Date.new(@year, 12, 31) << (12-@month)).day

    ## Build a list of matching weekdays
    offset = (weekday_num - first_weekday) % 7
    weekdays = ((offset+1)..(days_in_month)).step(7).to_a

    if schedule == :teenth
      day_of_month = weekdays.find {|x| x.between?(13, 19)}
    else
      day_of_month = weekdays[INDICES[schedule]]
    end

    return Date.new(@year, @month, day_of_month)
  end
end

@tnordloh thinks this looks great

Comments


You're not logged in right now. Please login via GitHub to comment