Class: Weekender

Inherits:
Object
  • Object
show all
Includes:
WeekenderHelpers
Defined in:
lib/weekender/weekender.rb

Constant Summary collapse

DAY_NAMES =
[ 'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday' ]

Instance Attribute Summary collapse

Attributes included from WeekenderHelpers

#date_format, #name_format

Instance Method Summary collapse

Methods included from WeekenderHelpers

#display

Constructor Details

#initialize(options = {}) ⇒ Weekender

Returns a new instance of Weekender.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/weekender/weekender.rb', line 23

def initialize(options={})
  @year       = options[:year]      || Date.today.year
  @month      = options[:month]     || Date.today.month
  @day        = options[:day]       || Date.today.day
  @year_end   = options[:year_end]  || year
  @month_end  = options[:month_end] || month
  @day_end    = options[:day_end]   || day
  @before     = options[:before]    || 0
  @after      = options[:after]     || 0
  @events     = options[:events]    || []
end

Instance Attribute Details

#afterObject

Returns the value of attribute after.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def after
  @after
end

#beforeObject

Returns the value of attribute before.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def before
  @before
end

#dayObject

Returns the value of attribute day.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def day
  @day
end

#day_endObject

Returns the value of attribute day_end.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def day_end
  @day_end
end

#eventsObject

Returns the value of attribute events.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def events
  @events
end

#monthObject

Returns the value of attribute month.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def month
  @month
end

#month_endObject

Returns the value of attribute month_end.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def month_end
  @month_end
end

#yearObject

Returns the value of attribute year.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def year
  @year
end

#year_endObject

Returns the value of attribute year_end.



13
14
15
# File 'lib/weekender/weekender.rb', line 13

def year_end
  @year_end
end

Instance Method Details

#add_event(date, options = {}) ⇒ Object



63
64
65
# File 'lib/weekender/weekender.rb', line 63

def add_event(date, options={})
  @events << [date, options]
end

#content_on(date) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/weekender/weekender.rb', line 87

def content_on(date)
  content = ''
  events_on(date).each do |event|
    content << event[1][:content]
  end
  content
end

#dateObject

private



126
127
128
# File 'lib/weekender/weekender.rb', line 126

def date
  Date.new(year, month, day)
end

#date_on(week, day) ⇒ Object



59
60
61
# File 'lib/weekender/weekender.rb', line 59

def date_on(week, day)
  Date.commercial(week_date(week).cwyear, week_date(week).cweek, day)
end

#end_dateObject



130
131
132
# File 'lib/weekender/weekender.rb', line 130

def end_date
  Date.new(year_end, month_end, day_end)
end

#end_weekObject



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

def end_week
  end_date + (after * 7)
end

#ends_onObject



51
52
53
# File 'lib/weekender/weekender.rb', line 51

def ends_on
  Date.commercial(end_week.cwyear, end_week.cweek, 7)
end

#event_on?(date) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
# File 'lib/weekender/weekender.rb', line 67

def event_on?(date)
  if events.empty?
    false
  else
    events_dates.include?(date)
  end
end

#events_datesObject



75
76
77
# File 'lib/weekender/weekender.rb', line 75

def events_dates
  events.map { |event| event[0] }
end

#events_on(date) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/weekender/weekender.rb', line 79

def events_on(date)
  if event_on?(date)
    events.select { |event| event[0] == date }
  else
    []
  end
end

#html_classes_on(date) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/weekender/weekender.rb', line 95

def html_classes_on(date)
  classes = ''
  events_on(date).each do |event|
    classes << ' ' unless classes == ''
    classes << event[1][:html_class]
  end
  classes
end

#nextObject



114
115
116
117
118
119
120
121
122
# File 'lib/weekender/weekender.rb', line 114

def next
  Weekender.new(
    :year   => next_date.year,
    :month  => next_date.month,
    :day    => next_date.day,
    :before => before,
    :after  => after
  )
end

#next_dateObject



150
151
152
# File 'lib/weekender/weekender.rb', line 150

def next_date
  ends_on + 1 + (before * 7)
end

#previousObject



104
105
106
107
108
109
110
111
112
# File 'lib/weekender/weekender.rb', line 104

def previous
  Weekender.new(
    :year   => previous_date.year,
    :month  => previous_date.month,
    :day    => previous_date.day,
    :before => before,
    :after  => after
  )
end

#previous_dateObject



146
147
148
# File 'lib/weekender/weekender.rb', line 146

def previous_date
  starts_on - 1 - (after * 7)
end

#spanObject



39
40
41
# File 'lib/weekender/weekender.rb', line 39

def span
  ((spans_to - spans_from).to_i + 1) / 7
end

#spans_fromObject



43
44
45
# File 'lib/weekender/weekender.rb', line 43

def spans_from
  Date.commercial(date.cwyear, date.cweek, 1)
end

#spans_toObject



55
56
57
# File 'lib/weekender/weekender.rb', line 55

def spans_to
  Date.commercial(end_date.cwyear, end_date.cweek, 7)
end

#start_weekObject



134
135
136
# File 'lib/weekender/weekender.rb', line 134

def start_week
  date - (before * 7)
end

#starts_onObject



47
48
49
# File 'lib/weekender/weekender.rb', line 47

def starts_on
  Date.commercial(start_week.cwyear, start_week.cweek, 1)
end

#week_date(week) ⇒ Object



142
143
144
# File 'lib/weekender/weekender.rb', line 142

def week_date(week)
  start_week + ((week - 1) * 7)
end

#weeksObject



35
36
37
# File 'lib/weekender/weekender.rb', line 35

def weeks
  before + span + after
end