lib/head_music/sign.rb in head_music-0.23.4 vs lib/head_music/sign.rb in head_music-0.24.0
- old
+ new
@@ -6,18 +6,24 @@
class HeadMusic::Sign
include Comparable
attr_reader :identifier, :cents, :musical_symbol
+ delegate :ascii, :unicode, :html_entity, to: :musical_symbol
+
+ SIGN_DATA = [
+ { identifier: :sharp, ascii: '#', unicode: '♯', html_entity: '♯', cents: 100 },
+ { identifier: :flat, ascii: 'b', unicode: '♭', html_entity: '♭', cents: -100 },
+ { identifier: :natural, ascii: '', unicode: '♮', html_entity: '♮', cents: 0 },
+ { identifier: :double_sharp, ascii: 'x', unicode: '𝄪', html_entity: '𝄪', cents: 200 },
+ { identifier: :double_flat, ascii: 'bb', unicode: '𝄫', html_entity: '𝄫', cents: -200 },
+ ].freeze
+
+ SIGN_IDENTIFIERS = SIGN_DATA.map { |attributes| attributes[:identifier] }.freeze
+
def self.all
- @all ||= [
- new(identifier: :sharp, ascii: '#', unicode: '♯', html_entity: '♯', cents: 100),
- new(identifier: :flat, ascii: 'b', unicode: '♭', html_entity: '♭', cents: -100),
- new(identifier: :natural, ascii: '', unicode: '♮', html_entity: '♮', cents: 0),
- new(identifier: :double_sharp, ascii: '##', unicode: '𝄪', html_entity: '𝄪', cents: 200),
- new(identifier: :double_flat, ascii: 'bb', unicode: '𝄫', html_entity: '𝄫', cents: -200),
- ]
+ SIGN_DATA.map { |attributes| new(attributes) }
end
def self.symbols
@symbols ||= all.map { |sign| [sign.ascii, sign.unicode] }.flatten.reject { |s| s.nil? || s.empty? }
end
@@ -55,26 +61,27 @@
def semitones
cents / 100.0
end
+ SIGN_IDENTIFIERS.each do |key|
+ define_method(:"#{key}?") { identifier == key }
+ end
+
def to_s
unicode
end
def <=>(other)
other = HeadMusic::Sign.get(other)
cents <=> other.cents
end
- delegate :ascii, :html_entity, :unicode, to: :musical_symbol
-
private
def initialize(attributes)
@identifier = attributes[:identifier]
@cents = attributes[:cents]
-
@musical_symbol = HeadMusic::MusicalSymbol.new(
unicode: attributes[:unicode],
ascii: attributes[:ascii],
html_entity: attributes[:html_entity]
)