Class: TimeCalc::Sequence
- Inherits:
-
Object
- Object
- TimeCalc::Sequence
- 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.
Instance Attribute Summary collapse
-
#from ⇒ Value
readonly
Wrapped sequence start.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #each {|Date/Time/DateTime| ... } ⇒ Enumerator or self
-
#for(span, unit) ⇒ Sequence
Produces sequence ending at ‘from.+(span, unit)`.
-
#initialize(from:, to: nil, step: nil) ⇒ Sequence
constructor
A new instance of Sequence.
- #inspect ⇒ Object (also: #to_s)
- #step(span = nil, unit = nil) ⇒ Object
- #to(date_or_time = nil) ⇒ Object
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
#from ⇒ Value (readonly)
Returns 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 #each ⇒ Enumerator
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)`.
111 112 113 |
# File 'lib/time_calc/sequence.rb', line 111 def for(span, unit) to(from.+(span, unit)) end |
#inspect ⇒ Object 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 |