Class: EventCalendar
- Inherits:
-
Object
- Object
- EventCalendar
- Defined in:
- lib/kalindar/event_calendar.rb
Overview
Public facing methods should return Event Decorators, private methods can return “raw” RiCal::Component::Event s.
Instance Attribute Summary collapse
-
#calendars ⇒ Object
Returns the value of attribute calendars.
Instance Method Summary collapse
- #events_for_date(date) ⇒ Object
-
#events_in(start_date, end_date) ⇒ Object
Best optimization potential.
-
#events_per_day(start_date, end_date) ⇒ Object
Nother optimization potential.
- #find_by_uid(uuid) ⇒ Object
-
#find_events(date) ⇒ Object
Find (non-recuring) events that begin, end or cover the given day.
-
#initialize(filename) ⇒ EventCalendar
constructor
Given filename or array of filenames, initialize the calendar.
-
#read_file(filename, create) ⇒ Object
Opens calendar param: create if true, create empty calendar file if not existant.
-
#singular_events_for_month(year, month) ⇒ Object
Catches only certain events.
Constructor Details
#initialize(filename) ⇒ EventCalendar
Given filename or array of filenames, initialize the calendar.
10 11 12 13 14 15 16 17 |
# File 'lib/kalindar/event_calendar.rb', line 10 def initialize filename @calendars = [] if filename.class == Array filename.each {|file| read_file file, true} else read_file filename, true end end |
Instance Attribute Details
#calendars ⇒ Object
Returns the value of attribute calendars.
7 8 9 |
# File 'lib/kalindar/event_calendar.rb', line 7 def calendars @calendars end |
Instance Method Details
#events_for_date(date) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/kalindar/event_calendar.rb', line 46 def events_for_date date events = @calendars.map &:events events.select {|event| event_includes? event, date}.flatten events.map {|event| Event.new event } end |
#events_in(start_date, end_date) ⇒ Object
Best optimization potential
64 65 66 67 68 69 70 |
# File 'lib/kalindar/event_calendar.rb', line 64 def events_in start_date, end_date events = [] (start_date .. end_date).each do |day| events << find_events(day) end events.flatten end |
#events_per_day(start_date, end_date) ⇒ Object
Nother optimization potential
55 56 57 58 59 60 61 |
# File 'lib/kalindar/event_calendar.rb', line 55 def events_per_day start_date, end_date map = {} (start_date .. end_date).each do |day| (map[day] ||= []) << find_events(day) end map end |
#find_by_uid(uuid) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/kalindar/event_calendar.rb', line 93 def find_by_uid uuid # we want to pick only the first! whats the method? detect is one, find another @calendars.map(&:events).flatten.each do |event| return Event.new(event) if event.uid == uuid end nil end |
#find_events(date) ⇒ Object
Find (non-recuring) events that begin, end or cover the given day.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/kalindar/event_calendar.rb', line 73 def find_events date #events = @calendars.map &:events @calendars.map do |calendar| calendar.events.select { |event| # If end-date is a Date (vs DateTime) let it be # All day/multiple day events if event.dtstart.class == Date && event.dtend.class == Date event.dtstart.to_date == date || (event.dtstart < date && event.dtend > date) else event.dtstart.to_date == date || event.dtend.to_date == date || (event.dtstart < date && event.dtend > date) # occurrences need to be re-enabled #||!event.occurrences(:overlapping => [date, date +1]).empty? end } end.flatten.map do |event| Event.new event end # check flat_map enumerable method end |
#read_file(filename, create) ⇒ Object
Opens calendar param: create if true, create empty calendar file if not existant.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/kalindar/event_calendar.rb', line 21 def read_file filename, create if create && !File.exist?(filename) File.open(filename, 'w') do |file| (RiCal::Component::Calendar.new).export_to file end end @calendars << File.open(filename, 'r') do |file| RiCal.parse file end.flatten.map do |calendar| c = Calendar.new calendar c.filename = filename c end @calendars.flatten! end |
#singular_events_for_month(year, month) ⇒ Object
Catches only certain events
38 39 40 41 42 43 44 |
# File 'lib/kalindar/event_calendar.rb', line 38 def singular_events_for_month year, month @calendar.map do |calendar| calendar.events.select { |event| event_between? event, dtmonth_start(year, month), dtmonth_end(year, month) } end.flatten end |