Sha256: 50a407cd6956582d0c96b79b03a15c0079ca33e097122ce919f04689a077c535

Contents?: true

Size: 1.58 KB

Versions: 7

Compression:

Stored size: 1.58 KB

Contents

module Chronic
  class RepeaterHour < Repeater #:nodoc:
    HOUR_SECONDS = 3600 # 60 * 60

    def initialize(type, options = {})
      super
      @current_hour_start = nil
    end

    def next(pointer)
      super

      unless @current_hour_start
        case pointer
        when :future
          @current_hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour + 1)
        when :past
          @current_hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour - 1)
        end
      else
        direction = pointer == :future ? 1 : -1
        @current_hour_start += direction * HOUR_SECONDS
      end

      Span.new(@current_hour_start, @current_hour_start + HOUR_SECONDS)
    end

    def this(pointer = :future)
      super

      case pointer
      when :future
        hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min + 1)
        hour_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour + 1)
      when :past
        hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
        hour_end = Chronic.construct(@now.year, @now.month, @now.day, @now.hour, @now.min)
      when :none
        hour_start = Chronic.construct(@now.year, @now.month, @now.day, @now.hour)
        hour_end = hour_start + HOUR_SECONDS
      end

      Span.new(hour_start, hour_end)
    end

    def offset(span, amount, pointer)
      direction = pointer == :future ? 1 : -1
      span + direction * amount * HOUR_SECONDS
    end

    def width
      HOUR_SECONDS
    end

    def to_s
      super << '-hour'
    end
  end
end

Version data entries

7 entries across 7 versions & 4 rubygems

Version Path
gitlab-chronic-0.10.6 lib/chronic/repeaters/repeater_hour.rb
gitlab-chronic-0.10.5 lib/chronic/repeaters/repeater_hour.rb
asana2flowdock-1.0.0 vendor/bundle/ruby/1.9.1/gems/chronic-0.10.2/lib/chronic/repeaters/repeater_hour.rb
chronic-mmlac-0.10.2.1 lib/chronic/repeaters/repeater_hour.rb
chronic-0.10.2 lib/chronic/repeaters/repeater_hour.rb
chronic-0.10.1 lib/chronic/repeaters/repeater_hour.rb
chronic-0.10.0 lib/chronic/repeaters/repeater_hour.rb