Class: Cyclotone::TimeSpan

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclotone/time_span.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, stop_time) ⇒ TimeSpan

Returns a new instance of TimeSpan.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
13
14
# File 'lib/cyclotone/time_span.rb', line 7

def initialize(start_time, stop_time)
  @start = coerce_time(start_time)
  @stop = coerce_time(stop_time)

  raise ArgumentError, "stop time must be greater than or equal to start time" if @stop < @start

  freeze
end

Instance Attribute Details

#startObject (readonly)

Returns the value of attribute start.



5
6
7
# File 'lib/cyclotone/time_span.rb', line 5

def start
  @start
end

#stopObject (readonly)

Returns the value of attribute stop.



5
6
7
# File 'lib/cyclotone/time_span.rb', line 5

def stop
  @stop
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



89
90
91
# File 'lib/cyclotone/time_span.rb', line 89

def ==(other)
  other.is_a?(self.class) && start == other.start && stop == other.stop
end

#cycle_numberObject



24
25
26
# File 'lib/cyclotone/time_span.rb', line 24

def cycle_number
  start.floor
end

#cycle_spans(max_cycles: nil) ⇒ Object



62
63
64
# File 'lib/cyclotone/time_span.rb', line 62

def cycle_spans(max_cycles: nil)
  each_cycle_span(max_cycles: max_cycles).to_a
end

#durationObject



16
17
18
# File 'lib/cyclotone/time_span.rb', line 16

def duration
  stop - start
end

#each_cycle_span(max_cycles: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cyclotone/time_span.rb', line 43

def each_cycle_span(max_cycles: nil)
  return enum_for(:each_cycle_span, max_cycles: max_cycles) unless block_given?
  return self if duration.zero?

  current_start = start
  emitted = 0

  while current_start < stop
    raise ArgumentError, "span crosses more than #{max_cycles} cycles" if max_cycles && emitted >= max_cycles

    cycle_boundary = [Rational(current_start.floor + 1), stop].min
    yield self.class.new(current_start, cycle_boundary)
    current_start = cycle_boundary
    emitted += 1
  end

  self
end

#hashObject



95
96
97
# File 'lib/cyclotone/time_span.rb', line 95

def hash
  [self.class, start, stop].hash
end

#includes?(time) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/cyclotone/time_span.rb', line 38

def includes?(time)
  normalized_time = coerce_time(time)
  start <= normalized_time && normalized_time < stop
end

#intersection(other) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/cyclotone/time_span.rb', line 28

def intersection(other)
  other_span = coerce_span(other)
  intersection_start = [start, other_span.start].max
  intersection_stop = [stop, other_span.stop].min

  return nil if intersection_start >= intersection_stop

  self.class.new(intersection_start, intersection_stop)
end

#midpointObject



20
21
22
# File 'lib/cyclotone/time_span.rb', line 20

def midpoint
  (start + stop) / 2
end

#reverse_within(cycle_start, cycle_length = 1) ⇒ Object

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
86
87
# File 'lib/cyclotone/time_span.rb', line 79

def reverse_within(cycle_start, cycle_length = 1)
  normalized_start = coerce_time(cycle_start)
  normalized_length = coerce_time(cycle_length)
  raise ArgumentError, "cycle length must be positive" unless normalized_length.positive?

  mirror = (normalized_start * 2) + normalized_length

  self.class.new(mirror - stop, mirror - start)
end

#scale(factor) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# File 'lib/cyclotone/time_span.rb', line 72

def scale(factor)
  normalized_factor = coerce_time(factor)
  raise ArgumentError, "scale factor must be non-negative" if normalized_factor.negative?

  self.class.new(start * normalized_factor, stop * normalized_factor)
end

#shift(amount) ⇒ Object



66
67
68
69
70
# File 'lib/cyclotone/time_span.rb', line 66

def shift(amount)
  normalized_amount = coerce_time(amount)

  self.class.new(start + normalized_amount, stop + normalized_amount)
end

#to_sObject



99
100
101
# File 'lib/cyclotone/time_span.rb', line 99

def to_s
  "[#{start}, #{stop})"
end