Class: WeeklyCalendar::Calendar

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Calendar

Returns a new instance of Calendar.



11
12
13
14
15
16
17
# File 'lib/weekly_calendar/calendar.rb', line 11

def initialize(options)
  @options = options
  
  @view = options[:view]
  @date = options[:for] || options[:date] || Date.current
  @events = options[:events] || {}
end

Instance Attribute Details

#output_bufferObject

Returns the value of attribute output_buffer.



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

def output_buffer
  @output_buffer
end

Class Method Details

.as_html(options) ⇒ Object



7
8
9
# File 'lib/weekly_calendar/calendar.rb', line 7

def self.as_html(options)
  self.new(options).to_html
end

Instance Method Details

#bodyObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/weekly_calendar/calendar.rb', line 51

def body
   :tr do
    String.new.html_safe.tap do |s|
      @days.each do |date|
        others = [ class: date_classes(date), :'data-date' => date.to_date ]
        s << (:td,
                          date_box(date) + events_ending_this_week(days, date) + events_for_date(date),
                          *others)
      end
    end
  end
end

#content_tag(*args, &block) ⇒ Object



104
105
106
# File 'lib/weekly_calendar/calendar.rb', line 104

def (*args, &block)
  @view.(*args, &block)
end

#date_box(date) ⇒ Object



64
65
66
# File 'lib/weekly_calendar/calendar.rb', line 64

def date_box(date)
   :div, date.mday, class: 'wc-date'
end

#daysObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/weekly_calendar/calendar.rb', line 27

def days
  start_week = (@date - 1.week)
  if ![0,6].include?(@date.wday) # is a weekday
    start_week -= 1.week
  end
  if @options[:weekend]
    start_week.week
  else
    start_week.week[1..5]
  end
end

#events_ending_this_week(week, date) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/weekly_calendar/calendar.rb', line 83

def events_ending_this_week(week, date)
  unless (events = events_ending_on(date)).empty?
     :div, class: 'wc-events-ending' do
      safe_str do |s|            
        events.compact.each do |event|
          next if event.start_at_date >= week.first.to_date              
          s << render_event(event, on: date.to_date)
        end
      end
    end
  end
end

#events_for_date(date) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/weekly_calendar/calendar.rb', line 68

def events_for_date(date)
  events = @events.delete(date.to_date)
  return if events.nil?
  
   :div, class: 'wc-events' do
    safe_str do |s|
      events.each do |event|
        next if event.nil?

        s << render_event(event, on: date.to_date)
      end
    end
  end
end

#headingsObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/weekly_calendar/calendar.rb', line 39

def headings
   :tr do
    String.new.html_safe.tap do |s|
      @days.each do |d|
        # @todo Localize the month/day order
        others = [ class: date_classes(d) ]
        s << (:th, "#{Date::ABBR_DAYNAMES[d.wday]} #{d.month}/#{d.mday}", *others)
      end
    end
  end
end

#safe_str(&block) ⇒ Object



100
101
102
# File 'lib/weekly_calendar/calendar.rb', line 100

def safe_str(&block)
  String.new.html_safe.tap(&block)
end

#to_htmlObject



96
97
98
# File 'lib/weekly_calendar/calendar.rb', line 96

def to_html
  to_s.html_safe
end

#to_sObject



19
20
21
22
23
24
25
# File 'lib/weekly_calendar/calendar.rb', line 19

def to_s
  @days = days
  
   :table, class: "weekly-calendar #{@options[:class]}", id: @options[:id] do
    (:thead, headings) + (:tbody, body)
  end
end