Sha256: ffc3ae70da9a0f62884b1e2310cd5017ac0c693d8be7c8ba1c1f9f4f573f8bfa

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 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
        @february_29 = month == 2 and day == 29
      end
      
    protected
      
      
      attr_reader :month, :day, :year
      
      def february_29?
        @february_29
      end
      
      
      
      def advance!
        @year += skip
        to_date!
      end
      
      def rewind!
        @year -= skip
        to_date!
      end
      
      
      
      def first_occurrence_on_or_after(date)
        @year = date.year
        @year += skip if (date.month > month) or (date.month == month and date.day > day)
        
        remainder = (@year - start_date.year) % skip
        @year += (skip - remainder) if remainder > 0
        
        to_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
        
        to_date!
      end
      
      
      
      def to_date!
        return Date.new(year, month, 28) if february_29? and !leap_year?(year)
        Date.new(year, month, day)
      end
      
      
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hiccup-0.5.0 lib/hiccup/enumerable/annually_enumerator.rb