Sha256: 9c9d67fc73394f882c6699437ae315b21a45ae21f8adde5b54b75cc24a2e5227

Contents?: true

Size: 1.13 KB

Versions: 6

Compression:

Stored size: 1.13 KB

Contents

module Reservation
  module EventFilter
    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

    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
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
reservation-0.1.0 lib/reservation/event_filter.rb
reservation-0.0.6 lib/reservation/event_filter.rb
reservation-0.0.5 lib/reservation/event_filter.rb
reservation-0.0.4 lib/reservation/event_filter.rb
reservation-0.0.3 lib/reservation/event_filter.rb
reservation-0.0.2 lib/reservation/event_filter.rb