Sha256: 96be5c51458729f40531ca7a7596dfdb003dd2b50944db4b20e2a8d5d5aca122

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require 'yaml'

module Music
module Transcription

class Part
  include Validatable
  
  attr_accessor :start_dynamic, :dynamic_changes, :notes
  
  @@check_methods = [:ensure_start_dynamic, :ensure_dynamic_change_values_range ]
  def initialize start_dynamic, notes: [], dynamic_changes: {}
    @notes = notes
    @start_dynamic = start_dynamic
    @dynamic_changes = dynamic_changes
    
    yield(self) if block_given?
  end
  
  def validatables
    @notes + @dynamic_changes.values
  end
  
  def clone
    Marshal.load(Marshal.dump(self))
  end
  
  def ==(other)
    return (@notes == other.notes) &&
    (@start_dynamic == other.start_dynamic) &&
    (@dynamic_changes == other.dynamic_changes)
  end

  def duration
    return @notes.inject(0) { |sum, note| sum + note.duration }
  end
  
  def ensure_start_dynamic
    unless @start_dynamic.between?(0,1)
      raise RangeError, "start dynamic #{@start_dynamic} is not between 0 and 1"
    end
  end
  
  def ensure_dynamic_change_values_range
    outofrange = @dynamic_changes.values.select {|v| !v.value.between?(0,1) }
    if outofrange.any?
      raise RangeError, "dynamic change values #{outofrange} are not between 0 and 1"
    end
  end
end

end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
music-transcription-0.17.1 lib/music-transcription/model/part.rb
music-transcription-0.17.0 lib/music-transcription/model/part.rb
music-transcription-0.16.0 lib/music-transcription/model/part.rb
music-transcription-0.15.0 lib/music-transcription/model/part.rb
music-transcription-0.14.0 lib/music-transcription/model/part.rb
music-transcription-0.13.0 lib/music-transcription/model/part.rb
music-transcription-0.11.0 lib/music-transcription/model/part.rb