lib/head_music/sign.rb in head_music-0.24.0 vs lib/head_music/sign.rb in head_music-0.24.1
- old
+ new
@@ -4,26 +4,41 @@
# A Sign is a symbol that modifies pitch, such as a sharp, flat, or natural.
class HeadMusic::Sign
include Comparable
- attr_reader :identifier, :cents, :musical_symbol
+ attr_reader :identifier, :cents, :musical_symbols
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 },
+ SIGN_RECORDS = [
+ {
+ identifier: :sharp, cents: 100,
+ symbols: [{ ascii: '#', unicode: '♯', html_entity: '♯' }],
+ },
+ {
+ identifier: :flat, cents: -100,
+ symbols: [{ ascii: 'b', unicode: '♭', html_entity: '♭' }],
+ },
+ {
+ identifier: :natural, cents: 0,
+ symbols: [{ ascii: '', unicode: '♮', html_entity: '♮' }],
+ },
+ {
+ identifier: :double_sharp, cents: 200,
+ symbols: [{ ascii: 'x', unicode: '𝄪', html_entity: '𝄪' }],
+ },
+ {
+ identifier: :double_flat, cents: -200,
+ symbols: [{ ascii: 'bb', unicode: '𝄫', html_entity: '𝄫' }],
+ },
].freeze
- SIGN_IDENTIFIERS = SIGN_DATA.map { |attributes| attributes[:identifier] }.freeze
+ SIGN_IDENTIFIERS = SIGN_RECORDS.map { |attributes| attributes[:identifier] }.freeze
def self.all
- SIGN_DATA.map { |attributes| new(attributes) }
+ SIGN_RECORDS.map { |attributes| new(attributes) }
end
def self.symbols
@symbols ||= all.map { |sign| [sign.ascii, sign.unicode] }.flatten.reject { |s| s.nil? || s.empty? }
end
@@ -74,19 +89,29 @@
def <=>(other)
other = HeadMusic::Sign.get(other)
cents <=> other.cents
end
+ def musical_symbol
+ musical_symbols.first
+ end
+
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]
- )
+ initialize_musical_symbols(attributes[:symbols])
+ end
+
+ def initialize_musical_symbols(list)
+ @musical_symbols = (list || []).map do |record|
+ HeadMusic::MusicalSymbol.new(
+ unicode: record[:unicode],
+ ascii: record[:ascii],
+ html_entity: record[:html_entity]
+ )
+ end
end
private_class_method :new
end