Class: CalendarHelper::Calendar

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Calendar

:first lets you set the first day to start the calendar on (default is the first day of the given :month and :year).

:first => :today will use Date.today

:last lets you set the last day of the calendar (default is the last day of the given :month and :year).

:last => :thirty will show 30 days from :first
:last => :week will show one week


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/table_builder/calendar_helper.rb', line 67

def initialize(options={})
  @year               = options[:year] || Time.now.year
  @month              = options[:month] || Time.now.month
  @first_day_of_week  = options[:first_day_of_week] || 0
  @first_weekday      = first_day_of_week(@first_day_of_week)
  @last_weekday       = last_day_of_week(@first_day_of_week)

  @first = options[:first]==:today ? Date.today : options[:first] || Date.civil(@year, @month, 1)

  if options[:last] == :thirty_days || options[:last] == :thirty
    @last = @first + 30
  elsif options[:last] == :one_week || options[:last] == :week
    @last = @first
  else
    @last = options[:last] || Date.civil(@year, @month, -1)
  end

end

Instance Attribute Details

#first_weekdayObject

Returns the value of attribute first_weekday



60
61
62
# File 'lib/table_builder/calendar_helper.rb', line 60

def first_weekday
  @first_weekday
end

#last_weekdayObject

Returns the value of attribute last_weekday



60
61
62
# File 'lib/table_builder/calendar_helper.rb', line 60

def last_weekday
  @last_weekday
end

#monthObject

Returns the value of attribute month



60
61
62
# File 'lib/table_builder/calendar_helper.rb', line 60

def month
  @month
end

Instance Method Details

#daysObject



122
123
124
125
126
127
128
# File 'lib/table_builder/calendar_helper.rb', line 122

def days
  unless @days
    @days = []
    each_day{|day| @days << day}
  end
  @days
end

#each_dayObject



86
87
88
89
90
# File 'lib/table_builder/calendar_helper.rb', line 86

def each_day
  first_day.upto(last_day) do |day|
    yield(day)
  end
end

#first_dayObject



100
101
102
103
104
105
106
# File 'lib/table_builder/calendar_helper.rb', line 100

def first_day
  first = @first - 6
  while(first.wday % 7 != (@first_weekday) % 7)
    first = first.next
  end
  first
end

#first_day_of_week(day) ⇒ Object



138
139
140
# File 'lib/table_builder/calendar_helper.rb', line 138

def first_day_of_week(day)
  day
end

#last_dayObject



92
93
94
95
96
97
98
# File 'lib/table_builder/calendar_helper.rb', line 92

def last_day
  last = @last
  while(last.wday % 7 != @last_weekday % 7)
    last = last.next
  end
  last
end

#last_day_of_week(day) ⇒ Object



142
143
144
145
146
147
148
# File 'lib/table_builder/calendar_helper.rb', line 142

def last_day_of_week(day)
  if day > 0
    day - 1
  else
    6
  end
end

#mjdaysObject



130
131
132
133
134
135
136
# File 'lib/table_builder/calendar_helper.rb', line 130

def mjdays
  unless @mjdays
    @mdays = []
    each_day{|day| @days << day}
  end
  @days
end

#objects_for_days(objects, day_method) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/table_builder/calendar_helper.rb', line 108

def objects_for_days(objects, day_method)
  unless @objects_for_days
    @objects_for_days = {}
    days.each{|day| @objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
    objects.each do |o|
      date = o.send(day_method.to_sym).strftime("%Y-%m-%d")
      if @objects_for_days[date]
        @objects_for_days[date][1] << o
      end
    end
  end
  @objects_for_days
end