lib/mass/pitch.rb in mass-0.0.1 vs lib/mass/pitch.rb in mass-0.0.2

- old
+ new

@@ -2,11 +2,14 @@ # Represents the pitch of a given note, and calculates its # correct MIDI value, leaving the +Note+ class more for # calculating the proper duration and actually playing out # the note. class Pitch - attr_reader :id, :name, :value + attr_reader :id + attr_reader :name + attr_reader :octave + attr_reader :value # A dictionary of MIDI note values that are substituted # for a given String note value. # # @type [Array<String>] @@ -35,29 +38,28 @@ # # @type [Array<Symbol>] REQUIRED = %i(name octave value) # @param [String] id - Identifier string of this pitch. - def initialize(identifier) - @id = identifier.to_s - @name = id.gsub(/\d/, '').to_s - @octave = id.gsub(/#{name}/, '').to_i + def initialize(id: '') + @id = id.to_s + @name = @id.gsub(/\d/, '').to_s + @octave = @id.gsub(/#{name}/, '').to_i @value = begin VALUES[name] rescue nil end end # Find a +Pitch+ by its given +id+. # + # @param [String] by_id # @return [Pitch] when the id is valid - # @throw [Pitch::NotFound] when id is not valid - def self.find(id) - pitch = new(id) - fail NotFound, id unless pitch.valid? - pitch + def self.find(by_id = nil) + return if by_id.nil? + new id: by_id end # Make sure the +name+ and +octave+ attributes have # been correctly parsed out of the given +id+. # @@ -71,9 +73,16 @@ # The MIDI value of this pitch. # # @return [Integer] def to_i value + octave_modifier + end + + # Use the +id+ parameter to define equivalence. + # + # @return [Boolean] whether both pitches have the same ID. + def ==(other) + other.id == id end private def required_params