Sha256: 847a794bf2004e461fc68cd9b6fe692753bc4b7e73a4495fbadc5b1651a143a0

Contents?: true

Size: 964 Bytes

Versions: 1

Compression:

Stored size: 964 Bytes

Contents

class HeadMusic::Octave
  include Comparable

  DEFAULT = 4

  MATCHER = /(-?\d+)$/

  def self.get(identifier)
    @octaves ||= {}
    from_number(identifier) || from_name(identifier) || default
  end

  def self.from_number(identifier)
    return nil unless identifier.to_s == identifier.to_i.to_s
    return nil unless (-2..12).include?(identifier.to_i)
    @octaves[identifier.to_i] ||= new(identifier.to_i)
  end

  def self.from_name(string)
    if string.to_s.match(HeadMusic::Spelling::MATCHER)
      _letter, _accidental, octave_string = string.to_s.match(HeadMusic::Spelling::MATCHER).captures
      @octaves[octave_string.to_i] ||= new(octave_string.to_i) if octave_string
    end
  end

  def self.default
    @octaves[DEFAULT] ||= new(DEFAULT)
  end

  attr_reader :number
  delegate :to_i, :to_s, to: :number

  def initialize(number)
    @number ||= number
  end

  def <=>(other)
    self.to_i <=> other.to_i
  end

  private_class_method :new
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
head_music-0.1.5 lib/head_music/octave.rb