Sha256: 03d288d6563e3297e5804fec3b78201ac2b36399abda87f0e21a81f1ebb3f2a6

Contents?: true

Size: 1.42 KB

Versions: 13

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

# A quality is a categorization of an interval.
class HeadMusic::Quality
  SHORTHAND = {
    perfect: 'P',
    major: 'M',
    minor: 'm',
    diminished: 'd',
    augmented: 'A',
    doubly_diminished: 'dd',
    doubly_augmented: 'AA',
  }.freeze
  NAMES = SHORTHAND.keys

  PERFECT_INTERVAL_MODIFICATION = {
    -2 => :doubly_diminished,
    -1 => :diminished,
    0 => :perfect,
    1 => :augmented,
    2 => :doubly_augmented,
  }.freeze

  MAJOR_INTERVAL_MODIFICATION = {
    -2 => :diminished,
    -1 => :minor,
    0 => :major,
    1 => :augmented,
    2 => :doubly_augmented,
  }.freeze

  def self.get(identifier)
    @qualities ||= {}
    identifier = identifier.to_s.to_sym
    @qualities[identifier] ||= new(identifier) if NAMES.include?(identifier)
  end

  def self.from(starting_quality, delta)
    if starting_quality == :perfect
      PERFECT_INTERVAL_MODIFICATION[delta].to_s.gsub(/_+/, ' ')
    elsif starting_quality == :major
      MAJOR_INTERVAL_MODIFICATION[delta].to_s.gsub(/_+/, ' ')
    end
  end

  attr_reader :name
  delegate :to_s, to: :name

  def initialize(name)
    @name = name
  end

  def ==(other)
    to_s == other.to_s
  end

  def shorthand
    SHORTHAND[name]
  end

  def article
    %w[a e i o u h].include?(name.to_s.first) ? 'an' : 'a'
  end

  NAMES.each do |method_name|
    define_method(:"#{method_name}?") { name == method_name }
  end

  private_class_method :new
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
head_music-0.24.1 lib/head_music/quality.rb
head_music-0.24.0 lib/head_music/quality.rb
head_music-0.23.4 lib/head_music/quality.rb
head_music-0.23.3 lib/head_music/quality.rb
head_music-0.23.2 lib/head_music/quality.rb
head_music-0.23.1 lib/head_music/quality.rb
head_music-0.23.0 lib/head_music/quality.rb
head_music-0.22.0 lib/head_music/quality.rb
head_music-0.20.0 lib/head_music/quality.rb
head_music-0.19.2 lib/head_music/quality.rb
head_music-0.19.1 lib/head_music/quality.rb
head_music-0.19.0 lib/head_music/quality.rb
head_music-0.18.0 lib/head_music/quality.rb