class Minutely::TimeRange

A class that represents a range of day times that is only using hours and minutes.

@!attribute [r] from

@return [Minutely::Time]

@!attribute [r] to

@return [Minutely::Time]

Attributes

from[R]
to[R]

Public Class Methods

new(from, to, exclude_end: false) click to toggle source

Builds a new `Minutely::TimeRange`.

@param from [Minutely::Time, String, Integer]

@param to [Minutely::Time, String, Integer]

@raise [ArgumentError] when first or second argument evaluates to `nil`.

# File lib/minutely/time_range.rb, line 30
def initialize(from, to, exclude_end: false)
  @from = Minutely::Time.parse(from)
  @to = Minutely::Time.parse(to)

  raise ArgumentError, 'invalid time range' if @from.nil? || @to.nil?

  @exclude_end = exclude_end
end
parse(value) click to toggle source

Parses the given input and returns a `Minutely::TimeRange` or `nil`, respectively.

@param value [Minutely::TimeRange, Array, Hash, String, nil]

@return [Minutely::TimeRange, nil]

@raise [ArgumentError] when the object does not represent a valid time

range

@raise [KeyError] when the given Hash does not contain the required keys (`:from` and `:to`)

# File lib/minutely/time_range.rb, line 60
def self.parse(value)
  TimeRange::Parser.parse(value)
end

Public Instance Methods

<=>(other) click to toggle source

Compares the time range to another one.

@return [Integer]

# File lib/minutely/time_range.rb, line 68
def <=>(other)
  return nil unless other.is_a?(self.class)
  return nil if exclude_end? != other.exclude_end?

  [from, to] <=> [other.from, other.to]
end
each() { |val| ... } click to toggle source

Iterates over all range elements and calls the given block for each element or returns a lazy enumerator when called without block.

@yield [time] A block called for every range element.

@yieldparam time [Minutely::Time] The range element.

@return [Enumerator, void]

# File lib/minutely/time_range.rb, line 94
def each
  return to_enum(:each) unless block_given?

  val = from

  loop do
    yield(val) if !exclude_end? || val != to
    break if val == to

    val = val.succ
  end
end
exclude_end?() click to toggle source

Indicates whether the range excludes the last item.

@return [Boolean]

# File lib/minutely/time_range.rb, line 43
def exclude_end?
  @exclude_end
end
include?(time) click to toggle source

Determines whether the specified time is in the range.

@return [Boolean]

# File lib/minutely/time_range.rb, line 79
def include?(time)
  time = Minutely::Time.parse(time)
  end_predicate = exclude_end? ? time < to : time <= to
  from <= time && end_predicate
end
spanning_midnight?() click to toggle source

Indicates whether the range spans midnight.

@return [Boolean]

# File lib/minutely/time_range.rb, line 111
def spanning_midnight?
  from > to
end
to_r() click to toggle source

Converts the time range into a Range. This does only work when the range spans midnight due to the way Ranges are based on value comparison.

@raise [RuntimeError] when the time range spans midnight. @return [Range]

# File lib/minutely/time_range.rb, line 121
def to_r
  raise 'Unable to convert ranges spanning midnight' if spanning_midnight?

  return from...to if exclude_end?

  from..to
end
to_s() click to toggle source

Converts the time range into a string.

@return [String]

# File lib/minutely/time_range.rb, line 133
def to_s
  [from, to].join('-')
end