Sha256: 8fb3cb64e14bb1d7f1b7fc6fbcb0910b6548e9b374c9a213b657e3065db4f2a6

Contents?: true

Size: 828 Bytes

Versions: 8

Compression:

Stored size: 828 Bytes

Contents

class HeadMusic::Accidental
  ACCIDENTAL_SEMITONES = {
    '#' => 1,
    '##' => 2,
    'b' => -1,
    'bb' => -2
  }

  attr_reader :string

  def self.get(identifier)
    for_symbol(identifier) || for_interval(identifier)
  end
  singleton_class.send(:alias_method, :[], :get)

  def self.for_symbol(identifier)
    @accidentals ||= {}
    @accidentals[identifier.to_s] ||= new(identifier.to_s) if ACCIDENTAL_SEMITONES.keys.include?(identifier.to_s)
  end

  def self.for_interval(semitones)
    @accidentals ||= {}
    @accidentals[semitones.to_i] ||= ACCIDENTAL_SEMITONES.key(semitones.to_i)
  end

  def initialize(string)
    @string = string
  end

  def to_s
    string
  end

  def ==(value)
    to_s == value.to_s
  end

  def semitones
    ACCIDENTAL_SEMITONES.fetch(string, 0)
  end

  private_class_method :new
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
head_music-0.8.0 lib/head_music/accidental.rb
head_music-0.7.0 lib/head_music/accidental.rb
head_music-0.6.4 lib/head_music/accidental.rb
head_music-0.6.3 lib/head_music/accidental.rb
head_music-0.6.1 lib/head_music/accidental.rb
head_music-0.6.0 lib/head_music/accidental.rb
head_music-0.5.4 lib/head_music/accidental.rb
head_music-0.5.3 lib/head_music/accidental.rb