lib/music-transcription/model/tempo.rb in music-transcription-0.17.1 vs lib/music-transcription/model/tempo.rb in music-transcription-0.19.0

- old
+ new

@@ -1,37 +1,27 @@ module Music module Transcription -# Represent the musical tempo, with beats ber minute and beat duration. class Tempo - include Comparable - attr_reader :beats_per_minute, :beat_duration - - def initialize beats_per_minute, beat_duration = Rational(1,4) - @beats_per_minute = beats_per_minute - @beat_duration = beat_duration + attr_reader :value + def initialize value + raise NonPositiveError, "Given tempo value #{value} is not positive" if value <= 0 + @value = value end def ==(other) - (other.beats_per_minute == @beats_per_minute) && - (other.beat_duration == @beat_duration) + self.class == other.class && self.value == other.value end - def notes_per_second - (@beats_per_minute * @beat_duration) / 60.0 - end - - def between? a, b - notes_per_second.between? a, b - end - - def <=>(other) - if other.is_a? Tempo - notes_per_second <=> other.notes_per_second - else - notes_per_second <=> other + [ :qnpm, :bpm, :npm, :nps ].each do |sym| + klass = Class.new(Tempo) do + def to_s + "#{@value}#{self.class::PRINT_SYM}" + end end + klass.const_set(:PRINT_SYM,sym) + Tempo.const_set(sym.upcase,klass) end end end -end +end \ No newline at end of file