Sha256: 8522addeae1c9d3292d37467ac95b6a20e9d3d0845c5ec712ce32c0a01bf9ec7

Contents?: true

Size: 1.21 KB

Versions: 4

Compression:

Stored size: 1.21 KB

Contents

require "timerage/version"
require "time"

module Timerage
  autoload :TimeInterval, "timerage/time_interval"

  # Returns a Time or Timerage::TimeInterval representation of the
  # iso8601 str.
  #
  # --
  #
  # Currently this only supports `<begin>/<end>` style time intervals.
  def self.parse_iso8601(str, exclusive_end: true)
    TimeInterval.iso8601(str, exclusive_end: exclusive_end)
  rescue ArgumentError
    Time.iso8601(str)
  end

  refine Range do
    def step(n, &blk)
      if self.begin.kind_of?(Time) || self.begin.kind_of?(Date)
        Timerage::TimeInterval.new(self).step(n, &blk)
      else
        super
      end
    end

    def to_time_interval
      Timerage::TimeInterval.new(self)
    end
  end
end

module Kernel
  def Timerage(time_or_time_interval_ish)
    thing = time_or_time_interval_ish

    case thing
    when ->(x) { x.respond_to? :to_time_interval }
      thing

    when ->(x) { x.respond_to? :exclude_end? }
      Timerage::TimeInterval.new(thing)

    when ->(x) { x.respond_to? :to_str }
      Timerage.parse_iso8601(thing.to_str)

    when ->(x) { x.respond_to? :to_time }
      thing.to_time

    else
      fail TypeError, "unable to coerce #{thing} to a time or interval"

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
timerage-2.2.0 lib/timerage.rb
timerage-2.1.1 lib/timerage.rb
timerage-2.1.0 lib/timerage.rb
timerage-2.0.0 lib/timerage.rb