Class: Calorie::Calendar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, month, data = {}) ⇒ Calendar

Returns a new instance of Calendar.



6
7
8
9
10
11
12
# File 'lib/calorie/calendar.rb', line 6

def initialize(year, month, data = {})
  @data = data
  @year = year
  @month = month
  initialize_days
  @weeks = WeeksInMonth.new(days).weeks
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/calorie/calendar.rb', line 5

def data
  @data
end

#daysObject (readonly)

Returns the value of attribute days.



5
6
7
# File 'lib/calorie/calendar.rb', line 5

def days
  @days
end

#monthObject (readonly)

Returns the value of attribute month.



5
6
7
# File 'lib/calorie/calendar.rb', line 5

def month
  @month
end

#weeksObject (readonly)

Returns the value of attribute weeks.



5
6
7
# File 'lib/calorie/calendar.rb', line 5

def weeks
  @weeks
end

#yearObject (readonly)

Returns the value of attribute year.



5
6
7
# File 'lib/calorie/calendar.rb', line 5

def year
  @year
end

Instance Method Details

#currentObject



52
53
54
# File 'lib/calorie/calendar.rb', line 52

def current
  Calorie.label_for(first_day)
end

#days_of_the_weekObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/calorie/calendar.rb', line 36

def days_of_the_week
  unless @days_of_the_week
    @days_of_the_week ||= (0..6).map do |i|
      DayOfTheWeek.new(i)
    end
    if Calorie.configuration.week_starts_on? :monday
      @days_of_the_week.push(@days_of_the_week.shift)
    end
  end
  @days_of_the_week
end

#each_day(&block) ⇒ Object



32
33
34
# File 'lib/calorie/calendar.rb', line 32

def each_day(&block)
  days.each {|day| block.call(day) }
end

#first_dayObject



14
15
16
17
# File 'lib/calorie/calendar.rb', line 14

def first_day
  @first_day ||= Date.new(year, month, 1)
  @first_day
end

#initialize_daysObject



24
25
26
27
28
29
30
# File 'lib/calorie/calendar.rb', line 24

def initialize_days
  @days = []
  (first_day.mday..last_day.mday).map do |i|
    date = Date.new(year, month, i)
    @days << Calorie::Day.new(date, data[i])
  end
end

#last_dayObject



19
20
21
22
# File 'lib/calorie/calendar.rb', line 19

def last_day
  @last_day ||= first_day.next_month.prev_day
  @last_day
end

#nextObject



56
57
58
# File 'lib/calorie/calendar.rb', line 56

def next
  Calorie.label_for(first_day.next_month)
end

#previousObject



48
49
50
# File 'lib/calorie/calendar.rb', line 48

def previous
  Calorie.label_for(first_day.prev_month)
end