Class: Week

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

Overview

TODO: - write RDoc documentation

- empty values as parameters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, week, fday = 0) ⇒ Week

Returns a new instance of Week.



14
15
16
# File 'lib/week.rb', line 14

def initialize(year, week, fday = 0)
  @year, @week, @fday = year, week, fday
end

Class Method Details

.nmonth(year) ⇒ Object



19
20
21
# File 'lib/week.rb', line 19

def nmonth year
  [31, (Date.new(year, 1, 1).leap? ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
end

Instance Method Details

#daysObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/week.rb', line 24

def days
  tmp_wday = @week * 7

  common_days, month_number = 0, 0

  Week.nmonth(@year).each_with_index do |month, number|
    if (common_days..(common_days + month)).member?(tmp_wday)
      month_number = number
      break
    end
    common_days += month
  end
  
  tmp_first_day = tmp_wday - common_days
  
  # fixing day number for calculate first week of the year
  tmp_first_day = tmp_first_day == 0 ? 1 : tmp_first_day
  
  # fixing problem with last week in month
  if tmp_first_day == -1
    month_number = 11
    tmp_first_day = Week.nmonth(@year)[month_number] - 2
  end
  
  # getting week listing
  days = []
  d = Time.local(@year, month_number + 1, tmp_first_day)
  first_day = d - d.wday.days
  @fday.upto(@fday + 6) do |day|
    days << Time.at(first_day + day.days)
  end

  days
end