Class: HeadMusic::Content::Placement

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/head_music/content/placement.rb

Overview

A placement is a note or rest at a position within a voice in a composition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(voice, position, rhythmic_value, pitch = nil) ⇒ Placement



13
14
15
# File 'lib/head_music/content/placement.rb', line 13

def initialize(voice, position, rhythmic_value, pitch = nil)
  ensure_attributes(voice, position, rhythmic_value, pitch)
end

Instance Attribute Details

#pitchObject (readonly)

Returns the value of attribute pitch.



8
9
10
# File 'lib/head_music/content/placement.rb', line 8

def pitch
  @pitch
end

#positionObject (readonly)

Returns the value of attribute position.



8
9
10
# File 'lib/head_music/content/placement.rb', line 8

def position
  @position
end

#rhythmic_valueObject (readonly)

Returns the value of attribute rhythmic_value.



8
9
10
# File 'lib/head_music/content/placement.rb', line 8

def rhythmic_value
  @rhythmic_value
end

#voiceObject (readonly)

Returns the value of attribute voice.



8
9
10
# File 'lib/head_music/content/placement.rb', line 8

def voice
  @voice
end

Instance Method Details

#<=>(other) ⇒ Object



29
30
31
# File 'lib/head_music/content/placement.rb', line 29

def <=>(other)
  position <=> other.position
end

#during?(other_placement) ⇒ Boolean



33
34
35
# File 'lib/head_music/content/placement.rb', line 33

def during?(other_placement)
  starts_during?(other_placement) || ends_during?(other_placement) || wraps?(other_placement)
end

#ends_during?(other_placement) ⇒ Boolean (private)



47
48
49
# File 'lib/head_music/content/placement.rb', line 47

def ends_during?(other_placement)
  next_position > other_placement.position && next_position <= other_placement.next_position
end

#ensure_attributes(voice, position, rhythmic_value, pitch) ⇒ Object (private)



55
56
57
58
59
60
# File 'lib/head_music/content/placement.rb', line 55

def ensure_attributes(voice, position, rhythmic_value, pitch)
  @voice = voice
  ensure_position(position)
  @rhythmic_value = HeadMusic::Rudiment::RhythmicValue.get(rhythmic_value)
  @pitch = HeadMusic::Rudiment::Pitch.get(pitch)
end

#ensure_position(position) ⇒ Object (private)



62
63
64
65
66
67
68
# File 'lib/head_music/content/placement.rb', line 62

def ensure_position(position)
  @position = if position.is_a?(HeadMusic::Content::Position)
    position
  else
    HeadMusic::Content::Position.new(composition, position)
  end
end

#next_positionObject



25
26
27
# File 'lib/head_music/content/placement.rb', line 25

def next_position
  @next_position ||= position + rhythmic_value
end

#note?Boolean



17
18
19
# File 'lib/head_music/content/placement.rb', line 17

def note?
  pitch
end

#rest?Boolean



21
22
23
# File 'lib/head_music/content/placement.rb', line 21

def rest?
  !note?
end

#starts_during?(other_placement) ⇒ Boolean (private)



43
44
45
# File 'lib/head_music/content/placement.rb', line 43

def starts_during?(other_placement)
  position >= other_placement.position && position < other_placement.next_position
end

#to_sObject



37
38
39
# File 'lib/head_music/content/placement.rb', line 37

def to_s
  "#{rhythmic_value} #{pitch || "rest"} at #{position}"
end

#wraps?(other_placement) ⇒ Boolean (private)



51
52
53
# File 'lib/head_music/content/placement.rb', line 51

def wraps?(other_placement)
  position <= other_placement.position && next_position >= other_placement.next_position
end