Sha256: 6f84219a160ca5d8aa12899a03fdabff55b4da179b1a975f890e769455142d5a

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

class HeadMusic::Placement
  include Comparable

  attr_reader :voice, :position, :rhythmic_value, :pitch
  delegate :composition, to: :voice
  delegate :spelling, to: :pitch, allow_nil: true

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

  def note?
    pitch
  end

  def rest?
    !note?
  end

  def next_position
    @next_position ||= position + rhythmic_value
  end

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

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

  private

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
head_music-0.14.5 lib/head_music/placement.rb
head_music-0.14.4 lib/head_music/placement.rb
head_music-0.14.2 lib/head_music/placement.rb
head_music-0.14.1 lib/head_music/placement.rb
head_music-0.13.2 lib/head_music/placement.rb