lib/music-transcription/model/change.rb in music-transcription-0.19.0 vs lib/music-transcription/model/change.rb in music-transcription-0.20.0

- old
+ new

@@ -1,10 +1,10 @@ module Music module Transcription class Change - attr_accessor :value, :duration + attr_reader :value, :duration def initialize value, duration @value = value @duration = duration end @@ -14,41 +14,69 @@ self.value == other.value && self.duration == other.duration end class Immediate < Change - include Validatable - def initialize value super(value,0) end - def check_methods - [ :ensure_zero_duration ] + def clone + Immediate.new(@value) end - def ensure_zero_duration - unless @duration == 0 - raise NonZeroError, "immediate change duration #{self.duration} must be 0" - end + def resize newdur + self.clone end end class Gradual < Change - include Validatable + def initialize value, transition_dur + if transition_dur <= 0 + raise NonPositiveError, "transition duration #{transition_dur} must be positive" + end + super(value, transition_dur) + end - def initialize value, transition_duration - super(value, transition_duration) + def clone + Gradual.new(@value,@duration) end - def check_methods - [ :ensure_nonnegative_duration ] + def resize newdur + Gradual.new(@value,newdur) end + end + + class Partial < Change + attr_reader :total_duration, :elapsed, :stop - def ensure_nonnegative_duration - if @duration < 0 - raise NegativeError, "gradual change duration #{self.duration} must be non-negative" + def initialize value, total_dur, elapsed, stop + if elapsed < 0 + raise NegativeError, "elapsed (#{elapsed}) is < 0" end + + if stop <= 0 + raise NonPositiveError, "stop (#{stop}) is < 0" + end + + if stop > total_dur + raise ArgumentError, "stop (#{stop}) is > total duration (#{total_dur})" + end + + if stop <= elapsed + raise ArgumentError, "stop (#{stop}) is <= elapsed (#{elapsed})" + end + + @total_duration = total_dur + @elapsed = elapsed + @stop = stop + super(value,stop - elapsed) + end + + def ==(other) + super() && + @elapsed == other.elapsed && + @stop == other.stop end end end end \ No newline at end of file