Class: Cyclotone::Event

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

Constant Summary collapse

UNSET =
Object.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(whole:, part:, value:) ⇒ Event

Returns a new instance of Event.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/cyclotone/event.rb', line 9

def initialize(whole:, part:, value:)
  raise ArgumentError, "part must be a TimeSpan" unless part.is_a?(TimeSpan)
  raise ArgumentError, "whole must be nil or a TimeSpan" unless whole.nil? || whole.is_a?(TimeSpan)

  @whole = whole
  @part = part
  @value = deep_freeze(value)
  freeze
end

Instance Attribute Details

#partObject (readonly)

Returns the value of attribute part.



7
8
9
# File 'lib/cyclotone/event.rb', line 7

def part
  @part
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/cyclotone/event.rb', line 7

def value
  @value
end

#wholeObject (readonly)

Returns the value of attribute whole.



7
8
9
# File 'lib/cyclotone/event.rb', line 7

def whole
  @whole
end

Instance Method Details

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



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

def ==(other)
  other.is_a?(self.class) &&
    whole == other.whole &&
    part == other.part &&
    value == other.value
end

#active_spanObject



41
42
43
# File 'lib/cyclotone/event.rb', line 41

def active_span
  whole || part
end

#covers_time?(time) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/cyclotone/event.rb', line 45

def covers_time?(time)
  active_span.includes?(time)
end

#durationObject



33
34
35
# File 'lib/cyclotone/event.rb', line 33

def duration
  whole&.duration
end

#has_whole?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cyclotone/event.rb', line 37

def has_whole?
  !whole.nil?
end

#hashObject



81
82
83
# File 'lib/cyclotone/event.rb', line 81

def hash
  [self.class, whole, part, value].hash
end

#offsetObject



23
24
25
# File 'lib/cyclotone/event.rb', line 23

def offset
  whole&.stop
end

#onsetObject



19
20
21
# File 'lib/cyclotone/event.rb', line 19

def onset
  whole&.start
end

#to_hObject



68
69
70
# File 'lib/cyclotone/event.rb', line 68

def to_h
  { whole: whole, part: part, value: value }
end

#triggered?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/cyclotone/event.rb', line 27

def triggered?
  return false unless onset

  part.includes?(onset)
end

#with_span(new_whole: UNSET, new_part: UNSET, whole: UNSET, part: UNSET) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cyclotone/event.rb', line 53

def with_span(new_whole: UNSET, new_part: UNSET, whole: UNSET, part: UNSET)
  next_whole = if whole.equal?(UNSET)
                 new_whole.equal?(UNSET) ? self.whole : new_whole
               else
                 whole
               end
  next_part = if part.equal?(UNSET)
                new_part.equal?(UNSET) ? self.part : new_part
              else
                part
              end

  self.class.new(whole: next_whole, part: next_part, value: value)
end

#with_value(new_value) ⇒ Object



49
50
51
# File 'lib/cyclotone/event.rb', line 49

def with_value(new_value)
  self.class.new(whole: whole, part: part, value: new_value)
end