Class: Timed::Item

Inherits:
Linked::Item
  • Object
show all
Includes:
Moment
Defined in:
lib/timed/item.rb

Overview

Item

The Timed Item is a Moment that can be chained to others to form a sequence. Importantly, items in a sequence are guaranteed to be sequential and non overlapping.

Instance Method Summary collapse

Methods included from Moment

#==, #after?, #before?, #duration, #during?, #inspect, #intersect

Constructor Details

#initialize(timespan, end_at = nil, sequence: nil) ⇒ Item

Creates a new Timed Item from a timespan. A timespan is any object that responds to #begin and #end with two numerical values. Note that the end must occur after the start for the span to be valid.

If given a second argument, the two will instead be interpreted as the begin and end time.

timespan - object responding to #begin and #end. end_at - if given, it togheter with the first argument will be used as the

begin and end time for the item.

sequence - optional sequence to add the item to.

Raises:

  • (TypeError)


35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/timed/item.rb', line 35

def initialize(timespan, end_at = nil, sequence: nil)
  if end_at
    begin_at = timespan
  else
    begin_at = timespan.begin
    end_at = timespan.end
  end

  raise TypeError unless begin_at.is_a?(Numeric) && end_at.is_a?(Numeric)
  raise ArgumentError if end_at < begin_at

  super begin_at..end_at, list: sequence
end

Instance Method Details

#append(other) ⇒ Object

Inserts an item after this one and before the next in the sequence. The new item may not overlap with the two it sits between. A RuntimeError will be raised if it does.

If the given object is an Item it will be inserted directly. Otherwise, if the object responds to #begin and #end, a new Item will be created from that timespan.

Raises:

  • (RuntimeError)


69
70
71
72
73
74
# File 'lib/timed/item.rb', line 69

def append(other)
  raise RuntimeError unless before? other
  raise RuntimeError unless last? || after?(self.next)

  super
end

#beginObject

Returns the time when the item starts.



51
52
53
# File 'lib/timed/item.rb', line 51

def begin
  offset value.begin
end

#endObject

Returns the time when the item ends.



57
58
59
# File 'lib/timed/item.rb', line 57

def end
  offset value.end
end

#prepend(other) ⇒ Object

Inserts an item before this one and after the previous in the sequence. The new item may not overlap with the two it sits between. A RuntimeError will be raised if it does.

If the given object is an Item it will be inserted directly. Otherwise, if the object responds to #begin and #end, a new Item will be created from that timespan.

Raises:

  • (RuntimeError)


84
85
86
87
88
89
# File 'lib/timed/item.rb', line 84

def prepend(other)
  raise RuntimeError unless after? other
  raise RuntimeError unless first? || before?(previous)

  super
end