Class: TimeCalc::Sequence

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/time_calc/sequence.rb

Overview

‘Sequence` is a Enumerable, allowing to iterate from start point in time over defined step, till end point or endlessly.

Examples:

seq = TimeCalc.(Time.parse('2019-06-01 14:50')).step(1, :day).for(2, :weeks)
# => #<TimeCalc::Sequence (2019-06-01 14:50:00 +0300 - 2019-06-15 14:50:00 +0300):step(1 day)>
seq.to_a
# => [2019-06-01 14:50:00 +0300, 2019-06-02 14:50:00 +0300, ....
seq.select(&:monday?)
# => [2019-06-03 14:50:00 +0300, 2019-06-10 14:50:00 +0300]

# Endless sequences are useful too:
seq = TimeCalc.(Time.parse('2019-06-01 14:50')).step(1, :day)
# => #<TimeCalc::Sequence (2019-06-01 14:50:00 +0300 - ...):step(1 day)>
seq.lazy.select(&:monday?).first(4)
# => [2019-06-03 14:50:00 +0300, 2019-06-10 14:50:00 +0300, 2019-06-17 14:50:00 +0300, 2019-06-24 14:50:00 +0300]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to: nil, step: nil) ⇒ Sequence

Note:

Prefer TimeCalc#to or TimeCalc#step for producing sequences.

Returns a new instance of Sequence.

Parameters:

  • from (Time, Date, DateTime)
  • to (Time, Date, DateTime, nil) (defaults to: nil)

    ‘nil` produces endless sequence, which can be limited later with #to method.

  • step ((Integer, Symbol), nil) (defaults to: nil)

    Pair of span and unit to advance sequence; no ‘step` produces incomplete sequence (#each will raise), which can be completed later with #step method.



32
33
34
35
36
# File 'lib/time_calc/sequence.rb', line 32

def initialize(from:, to: nil, step: nil)
  @from = Value.wrap(from)
  @to = to&.then(&Value.method(:wrap))
  @step = step
end

Instance Attribute Details

#fromValue (readonly)

Returns Wrapped sequence start.

Returns:

  • (Value)

    Wrapped sequence start.



22
23
24
# File 'lib/time_calc/sequence.rb', line 22

def from
  @from
end

Instance Method Details

#==(other) ⇒ Object



116
117
118
# File 'lib/time_calc/sequence.rb', line 116

def ==(other)
  other.is_a?(self.class) && from == other.from && to == other.to && step == other.step
end

#each {|Date/Time/DateTime| ... } ⇒ self #eachEnumerator

Overloads:

  • #each {|Date/Time/DateTime| ... } ⇒ self

    Yields:

    • (Date/Time/DateTime)

      Next element in sequence

    Returns:

    • (self)
  • #eachEnumerator

    Returns:

    • (Enumerator)

Yields:

  • (Date/Time/DateTime)

    Next element in sequence

Returns:

  • (Enumerator or self)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/time_calc/sequence.rb', line 53

def each
  fail TypeError, "No step defined for #{self}" unless @step

  return to_enum(__method__) unless block_given?

  return unless matching_direction?(@from)

  cur = @from
  while matching_direction?(cur)
    yield cur.unwrap
    cur = cur.+(*@step) # rubocop:disable Style/SelfAssignment
  end
  yield cur.unwrap if cur == @to

  self
end

#for(span, unit) ⇒ Sequence

Produces sequence ending at ‘from.+(span, unit)`.

Examples:

TimeCalc.(Time.parse('2019-06-01 14:50')).step(1, :day).for(2, :weeks).count
# => 15

Parameters:

  • span (Integer)
  • unit (Symbol)

    Any of supported units.

Returns:



111
112
113
# File 'lib/time_calc/sequence.rb', line 111

def for(span, unit)
  to(from.+(span, unit))
end

#inspectObject Also known as: to_s



39
40
41
42
# File 'lib/time_calc/sequence.rb', line 39

def inspect
  '#<%s (%s - %s):step(%s)>' %
    [self.class, @from.unwrap, @to&.unwrap || '...', @step&.join(' ') || '???']
end

#step(Integer, Symbol) #step(unit) ⇒ Sequence #step(span, unit) ⇒ Sequence

Overloads:

  • #step(Integer, Symbol)

    Returns current step.

    Returns:

    • ((Integer, Symbol))

      current step

  • #step(unit) ⇒ Sequence

    Shortcut for ‘step(1, unit)`

    Parameters:

    • unit (Symbol)

      Any of supported units.

    Returns:

  • #step(span, unit) ⇒ Sequence

    Produces new sequence with changed step.

    Parameters:

    • span (Ineger)
    • unit (Symbol)

      Any of supported units.

    Returns:



83
84
85
86
87
88
# File 'lib/time_calc/sequence.rb', line 83

def step(span = nil, unit = nil)
  return @step if span.nil?

  span, unit = 1, span if unit.nil?
  Sequence.new(from: @from, to: @to, step: [span, unit])
end

#toValue #to(date_or_time) ⇒ Sequence

Overloads:

  • #toValue

    Returns current sequence end, wrapped into Value.

    Returns:

    • (Value)

      current sequence end, wrapped into Value

  • #to(date_or_time) ⇒ Sequence

    Produces new sequence with end changed

    Parameters:

    • date_or_time (Date, Time, DateTime)

    Returns:



96
97
98
99
100
# File 'lib/time_calc/sequence.rb', line 96

def to(date_or_time = nil)
  return @to if date_or_time.nil?

  Sequence.new(from: @from, to: date_or_time, step: @step)
end