Sha256: 355f2247f62c4267a3e1f9a9e997b670646ba4eb0f1e473b2ce001fc644331c8

Contents?: true

Size: 838 Bytes

Versions: 1

Compression:

Stored size: 838 Bytes

Contents

module Crono
  class Period
    def initialize(period, at: nil)
      @period = period
      @at_hour, @at_min = parse_at(at) if at
    end

    def next(since: nil)
      if since.nil?
        @next = Time.now.change(time_atts)
        return @next if @next.future?
        since = Time.now
      end

      @period.since(since).change(time_atts)
    end

    def description
      desc = "every #{@period.inspect}"
      desc += " at %.2i:%.2i" % [@at_hour, @at_min] if @at_hour && @at_min
      desc
    end

    def parse_at(at)
      case at
      when String
        time = Time.parse(at)
        return time.hour, time.min
      when Hash
        return at[:hour], at[:min]
      else
        raise "Unknown 'at' format"
      end
    end

  private
    def time_atts
      {hour: @at_hour, min: @at_min}.compact
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crono-0.7.0 lib/crono/period.rb