lib/music-transcription/note.rb in music-transcription-0.7.1 vs lib/music-transcription/note.rb in music-transcription-0.7.2

- old
+ new

@@ -2,34 +2,35 @@ module Transcription require 'set' class Note - attr_reader :duration, :pitches, :links - attr_accessor :accent + include Validatable + + attr_reader :pitches, :links + attr_accessor :accent, :duration def initialize duration, pitches = [], links: {}, accent: Accents::NONE self.duration = duration @pitches = Set.new(pitches).sort @links = links - self.accent = accent + @duration = duration + @accent = accent + + @check_methods = [ :ensure_positive_duration ] end + def ensure_positive_duration + raise ValueNotPositiveError, "duration #{@duration} is not positive" if @duration <= 0 + end + def == other return (@duration == other.duration) && (self.pitches == other.pitches) && (@links.to_a.sort == other.links.to_a.sort) && (@accent == other.accent) end - - # Set the note duration. - # @param [Numeric] duration The duration to use. - # @raise [ArgumentError] if duration is not greater than 0. - def duration= duration - raise ValueNotPositiveError if duration <= 0 - @duration = duration - end def clone Marshal.load(Marshal.dump(self)) end @@ -57,9 +58,13 @@ end def stretch! ratio @duration *= ratio return self + end + + def valid? + @duration > 0 end class Sixteenth < Note def initialize pitches = [], links: {}, accent: Accents::NONE super(Rational(1,16),pitches,links:links,accent:accent)