Class: Calorie::WeeksInMonth

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

Instance Method Summary collapse

Constructor Details

#initialize(days = []) ⇒ WeeksInMonth

Returns a new instance of WeeksInMonth.



3
4
5
# File 'lib/calorie/weeks_in_month.rb', line 3

def initialize(days = [])
  @source_days = days
end

Instance Method Details

#blank_days_at_endObject



33
34
35
# File 'lib/calorie/weeks_in_month.rb', line 33

def blank_days_at_end
  7 - ((blank_days_at_start + days_in_month) % 7)
end

#blank_days_at_startObject



29
30
31
# File 'lib/calorie/weeks_in_month.rb', line 29

def blank_days_at_start
  first_day_falls_on
end

#days_for_slicingObject



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/calorie/weeks_in_month.rb', line 16

def days_for_slicing
  slices = @source_days.clone

  (1..blank_days_at_start).each do |i|
    slices.unshift(Calorie::NullDay.new(first_day - i))
  end
  (1..blank_days_at_end).each do |i|
    slices.push(Calorie::NullDay.new(last_day + i))
  end

  slices
end

#days_in_monthObject



41
42
43
# File 'lib/calorie/weeks_in_month.rb', line 41

def days_in_month
  @source_days.length
end

#first_dayObject



45
46
47
# File 'lib/calorie/weeks_in_month.rb', line 45

def first_day
  @source_days.first.date
end

#first_day_falls_onObject



53
54
55
56
57
58
59
# File 'lib/calorie/weeks_in_month.rb', line 53

def first_day_falls_on
  if Calorie.configuration.week_starts_on?(:monday)
    (first_day.wday - 1) % 7
  else
    first_day.wday
  end
end

#last_dayObject



49
50
51
# File 'lib/calorie/weeks_in_month.rb', line 49

def last_day
  @source_days.last.date
end

#number_of_weeksObject



37
38
39
# File 'lib/calorie/weeks_in_month.rb', line 37

def number_of_weeks
  ((days_in_month + first_day_falls_on) / 7.0).ceil
end

#weeksObject



7
8
9
10
11
12
13
14
# File 'lib/calorie/weeks_in_month.rb', line 7

def weeks
  result = []
  padded_days = days_for_slicing
  number_of_weeks.times do
    result << Calorie::Week.new(padded_days.slice!(0..6))
  end
  result
end