Sha256: 5d77407946871336134852bccc99dbad821c59ed8aad88bab803d3ace071f7f0

Contents?: true

Size: 1.32 KB

Versions: 21

Compression:

Stored size: 1.32 KB

Contents

class HeadMusic::Quality
  SHORTHAND = {
    perfect: 'P',
    major: 'M',
    minor: 'm',
    diminished: 'd',
    augmented: 'A',
    doubly_diminished: 'dd',
    doubly_augmented: 'AA',
  }
  NAMES = SHORTHAND.keys

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

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

  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)
    self.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

21 entries across 21 versions & 1 rubygems

Version Path
head_music-0.17.0 lib/head_music/quality.rb
head_music-0.16.2 lib/head_music/quality.rb
head_music-0.16.0 lib/head_music/quality.rb
head_music-0.14.8 lib/head_music/quality.rb
head_music-0.14.7 lib/head_music/quality.rb
head_music-0.14.5 lib/head_music/quality.rb
head_music-0.14.4 lib/head_music/quality.rb
head_music-0.14.2 lib/head_music/quality.rb
head_music-0.14.1 lib/head_music/quality.rb
head_music-0.13.2 lib/head_music/quality.rb
head_music-0.11.9 lib/head_music/quality.rb
head_music-0.11.8 lib/head_music/quality.rb
head_music-0.11.7 lib/head_music/quality.rb
head_music-0.11.6 lib/head_music/quality.rb
head_music-0.11.5 lib/head_music/quality.rb
head_music-0.11.4 lib/head_music/quality.rb
head_music-0.11.3 lib/head_music/quality.rb
head_music-0.11.2 lib/head_music/quality.rb
head_music-0.11.1 lib/head_music/quality.rb
head_music-0.10.0 lib/head_music/quality.rb