Sha256: ba73bf24dca903a9c1ddb5e5fc4ba3d043e72d42e74de4b33000af9c06087a23

Contents?: true

Size: 1.42 KB

Versions: 10

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

10 entries across 10 versions & 1 rubygems

Version Path
head_music-0.27.0 lib/head_music/quality.rb
head_music-0.26.3 lib/head_music/quality.rb
head_music-0.26.2 lib/head_music/quality.rb
head_music-0.26.1 lib/head_music/quality.rb
head_music-0.26.0 lib/head_music/quality.rb
head_music-0.25.0 lib/head_music/quality.rb
head_music-0.24.5 lib/head_music/quality.rb
head_music-0.24.4 lib/head_music/quality.rb
head_music-0.24.3 lib/head_music/quality.rb
head_music-0.24.2 lib/head_music/quality.rb