Sha256: fb31796f28d912f7a659874c6606614eb88ebfb1a9d9c146de81b0179583292b

Contents?: true

Size: 745 Bytes

Versions: 4

Compression:

Stored size: 745 Bytes

Contents

module Zhong
  class Every
    class FailedToParse < StandardError; end

    EVERY_KEYWORDS = {
      day: 1.day,
      week: 1.week,
      month: 1.month,
      semiannual: 6.months, # enterprise!
      year: 1.year,
      decade: 10.year
    }.freeze

    def initialize(period)
      @period = period
    end

    def next_at(last = Time.now)
      last + @period
    end

    def self.parse(every)
      return unless every

      case every
      when Numeric, ActiveSupport::Duration
        new(every)
      when String, Symbol
        key = every.downcase.to_sym

        fail FailedToParse, every unless EVERY_KEYWORDS.key?(key)

        new(EVERY_KEYWORDS[key])
      else
        fail FailedToParse, every
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zhong-0.1.3 lib/zhong/every.rb
zhong-0.1.2 lib/zhong/every.rb
zhong-0.1.1 lib/zhong/every.rb
zhong-0.1.0 lib/zhong/every.rb