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.



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)



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

Yields:

  • (Date/Time/DateTime)

    Next element in sequence



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


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(unit) ⇒ Sequence

    Shortcut for ‘step(1, unit)`

  • #step(span, unit) ⇒ Sequence

    Produces new sequence with changed step.



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:

  • #to(date_or_time) ⇒ Sequence

    Produces new sequence with end changed



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