Sha256: 2bf084f9753d336d00c00010159a23baed83763fd882f80414674bbb206ad42b

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

class HeadMusic::DiatonicInterval::Parser
  NUMBER_NAMES = HeadMusic::DiatonicInterval::NUMBER_NAMES

  attr_reader :identifier

  def initialize(identifier)
    @identifier = expand(identifier)
  end

  def words
    identifier.to_s.split(/[_ ]+/)
  end

  def quality_name
    words[0..-2].join(" ").to_sym
  end

  def degree_name
    words.last
  end

  def steps
    NUMBER_NAMES.index(degree_name)
  end

  def higher_letter
    HeadMusic::Pitch.middle_c.letter_name.steps_up(steps)
  end

  def expand(identifier)
    if /[A-Z]\d{1,2}/i.match?(identifier)
      number = NUMBER_NAMES[identifier.gsub(/[A-Z]/i, "").to_i - 1]
      return [quality_for(identifier[0]), number].join("_").to_sym
    end
    identifier
  end

  def quality_abbreviations
    HeadMusic::DiatonicInterval::QUALITY_ABBREVIATIONS
  end

  def quality_for(abbreviation)
    quality_abbreviations[abbreviation.to_sym] ||
      quality_abbreviations[abbreviation.upcase.to_sym] ||
      quality_abbreviations[abbreviation.downcase.to_sym]
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
head_music-7.0.5 lib/head_music/diatonic_interval/parser.rb
head_music-7.0.4 lib/head_music/diatonic_interval/parser.rb
head_music-7.0.3 lib/head_music/diatonic_interval/parser.rb
head_music-7.0.2 lib/head_music/diatonic_interval/parser.rb
head_music-7.0.1 lib/head_music/diatonic_interval/parser.rb
head_music-7.0.0 lib/head_music/diatonic_interval/parser.rb