Sha256: 5c29b031db197a9c068f9799074eaccfc9ad359e0c7f76c9f2ea2a63e3517a49

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 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

  # Returns a new TimeInterval from a time and duration or two times.
  #
  # signature:
  #  Interval(begin_time, end_time, exclude_end: true)
  #  Interval(begin_or_end_time, duration, exclude_end: true)
  def self.Interval(begin_or_end_time, end_time_or_duration, exclude_end: true)
    if end_time_or_duration.respond_to?(:since)
      TimeInterval.from_time_and_duration(begin_or_end_time, end_time_or_duration, exclude_end: exclude_end)
    else
      TimeInterval.new(begin_or_end_time, end_time_or_duration, exclude_end)
    end
  end

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

    def to_time_interval
      Timerage::TimeInterval.from_range(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.from_range(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

1 entries across 1 versions & 1 rubygems

Version Path
timerage-2.3.0 lib/timerage.rb