Sha256: 4f7bf04ead82933e181b0814b85d1dc4ee29461cb4c52638877e2ae0af14cffe

Contents?: true

Size: 828 Bytes

Versions: 3

Compression:

Stored size: 828 Bytes

Contents

require 'core_ext/date'

module Reservation
  module Schedule
    #
    # a utility class to represent an interval starting on a day
    #
    class Daily
      attr_accessor :wday, :nth_of_month, :interval

      def initialize wday, nth_of_month, interval
        @wday = wday
        @nth_of_month = nth_of_month
        @interval = interval
      end

      def matches? event
        good_day?(event.start.to_date) && interval.matches?(event)
      end

      def generate date, list
        return list unless good_day?(date)
        list << interval.generate(date)
        list
      end

      def good_day? date
        (date.wday == self.wday) && ((self.nth_of_month == :all) || date.nth_day_of_month?(self.nth_of_month))
      end

      def to_s
        "#{MAP_DAY[wday]} => #{interval}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
reservation-0.1.0 lib/reservation/daily.rb
reservation-0.0.6 lib/reservation/daily.rb
reservation-0.0.5 lib/reservation/daily.rb