Module: Reservation::EventFilter

Included in:
Event
Defined in:
lib/reservation/event_filter.rb

Instance Method Summary collapse

Instance Method Details

#filter_events(options) ⇒ Object



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
# File 'lib/reservation/event_filter.rb', line 13

def filter_events options
  from = options["from"]
  upto = options["upto"]
  context = options["context"]
  schedule = options["schedule"]

  events = ::Reservation.events

  if from
    from = from.is_a?(String) ? Date.parse(from) : from.to_date
    events = events.since(from)
  end

  if upto
    upto = upto.is_a?(String) ? parse_time_for_upto(upto) : upto.to_time if upto
    events = events.upto(upto) if upto
  end

  if context
    context = [context] unless context.is_a? Array
    context = context.uniq
    events = context.inject(events) { |ee, ctx|
      ee.reserved_for(ctx)
    }
  end

  if schedule
    schedule = ::Reservation::Schedule::Weekly.new schedule
    events = schedule.filter events
  end

  events
end

#parse_time_for_upto(txt) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/reservation/event_filter.rb', line 3

def parse_time_for_upto txt
  d = Date.parse(txt) rescue nil
  t = Time.parse(txt) rescue nil
  raise "can't read date/time #{txt.inspect}" if d.nil? && t.nil?
  if d && (d.to_time == t)
    return (d + 1.day).to_time
  end
  t
end