Sha256: 4b31630cf4fb265748e53a59d9e1a68e1bc5657dcccb313b7d31378b4d1aff13

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

require 'hiccup/enumerable/schedule_enumerator'

module Hiccup
  module Enumerable
    class AnnuallyEnumerator < ScheduleEnumerator
      
      def initialize(*args)
        super
        @month, @day = start_date.month, start_date.day
        
        if month == 2 and day == 29
          def self.current_date
            Date.new(year, 2, leap_year?(year) ? 29 : 28)
          end
        end
      end
      
      attr_reader :month, :day
      
    protected
      
      attr_reader :year
      
      
      
      def advance!
        @year += skip
        current_date
      end
      
      def rewind!
        @year -= skip
        current_date
      end
      
      
      
      def first_occurrence_on_or_after(date)
        @year = date.year
        @year += 1 if (date.month > month) or (date.month == month and date.day > day)
        
        remainder = (@year - start_date.year) % skip
        @year += (skip - remainder) if remainder > 0
        
        current_date
      end
      
      def first_occurrence_on_or_before(date)
        @year = date.year
        @year -= 1 if (date.month < month) or (date.month == month and date.day < day)
        
        remainder = (@year - start_date.year) % skip
        @year -= remainder if remainder > 0
        
        current_date
      end
      
      
      
      def current_date
        Date.new(year, month, day)
      end
      
      
      
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hiccup-0.5.4 lib/hiccup/enumerable/annually_enumerator.rb
hiccup-0.5.3 lib/hiccup/enumerable/annually_enumerator.rb
hiccup-0.5.2 lib/hiccup/enumerable/annually_enumerator.rb
hiccup-0.5.1 lib/hiccup/enumerable/annually_enumerator.rb