summaryrefslogtreecommitdiff
path: root/helpers/calendar_helpers.rb
blob: 3b915dc95af9b041ba7badf46fa07d66d2e7e87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'date'

module CalendarHelpers

  FORMATS = {
    time_only: "%H:%M",
    date_only: "%a. %d.%m",
    date_string_short: "%e. %b %Y",
    log: "%Y-%m-%d %H:%M"
  }

  class Event

    # TODO i18n weekdays, group dates

    def initialize(args={})
      @summary    = args.fetch("summary")
      @start_time = DateTime.parse(args.fetch("start"))
      @end_time   = DateTime.parse(args.fetch("end"))
    end

    def summary
      @summary
    end

    def start_time
      @start_time.strftime(FORMATS[:time_only])
    end

    def end_time
      @end_time.strftime(FORMATS[:time_only])
    end

    def date
      @start_time.strftime(FORMATS[:date_only])
    end

  end

  def week_schedule
    events = data.calendar-week.map {|atts| Event.new(atts) }
    events.group_by {|ev| ev.date }
  end

  def events_on(datetime)
    date = datetime.to_date
    events = data.calendar.select do |hsh|
      ev_date = DateTime.parse(hsh['start']).to_date
      ev_date == date
    end
    events.map {|atts| Event.new(atts)}
  end

  def current_week
    monday = week_start(Date.parse("2014-11-18"))
    (monday..monday + 6).to_a
  end

  def week_start(date, offset_from_sunday=1)
    date - (date.wday - offset_from_sunday) % 7
  end

end