Sha256: b4fd279d56e5d57526aa7f00efc3730719547740024383f3a0194030ccc9c02e

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 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)
    case starting_quality
    when :perfect
      PERFECT_INTERVAL_MODIFICATION[delta].to_s.gsub(/_+/, " ")
    when :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

9 entries across 9 versions & 1 rubygems

Version Path
head_music-4.0.1 lib/head_music/quality.rb
head_music-4.0.0 lib/head_music/quality.rb
head_music-3.0.1 lib/head_music/quality.rb
head_music-3.0.0 lib/head_music/quality.rb
head_music-2.0.1 lib/head_music/quality.rb
head_music-2.0.0 lib/head_music/quality.rb
head_music-1.0.0 lib/head_music/quality.rb
head_music-0.29.0 lib/head_music/quality.rb
head_music-0.28.0 lib/head_music/quality.rb