lib/head_music/sign.rb in head_music-0.17.0 vs lib/head_music/sign.rb in head_music-0.18.0

- old
+ new

@@ -1,17 +1,20 @@ +# frozen_string_literal: true + +# A Sign is a symbol that modifies pitch, such as a sharp, flat, or natural. class HeadMusic::Sign include Comparable - attr_reader :identifier, :name, :ascii, :unicode, :html_entity, :cents + attr_reader :identifier, :ascii, :unicode, :html_entity, :cents def self.all @all ||= [ - new(identifier: :sharp, name: 'sharp', ascii: '#', unicode: '♯', html_entity: '&#9839;', cents: 100), - new(identifier: :flat, name: 'flat', ascii: 'b', unicode: "♭", html_entity: '&#9837;', cents: -100), - new(identifier: :natural, name: 'natural', ascii: '', unicode: '♮', html_entity: '&#9838;', cents: 0), - new(identifier: :double_sharp, name: 'double sharp', ascii: '##', unicode: '𝄪', html_entity: '&#119082;', cents: 200), - new(identifier: :double_flat, name: 'double flat', ascii: 'bb', unicode: '𝄫', html_entity: '&#119083;', cents: -200), + new(identifier: :sharp, ascii: '#', unicode: '♯', html_entity: '&#9839;', cents: 100), + new(identifier: :flat, ascii: 'b', unicode: '♭', html_entity: '&#9837;', cents: -100), + new(identifier: :natural, ascii: '', unicode: '♮', html_entity: '&#9838;', cents: 0), + new(identifier: :double_sharp, ascii: '##', unicode: '𝄪', html_entity: '&#119082;', cents: 200), + new(identifier: :double_flat, ascii: 'bb', unicode: '𝄫', html_entity: '&#119083;', cents: -200), ] end def self.symbols @sign_symbols ||= all.map { |sign| [sign.ascii, sign.unicode] }.flatten.reject { |s| s.nil? || s.empty? } @@ -20,11 +23,11 @@ def self.matcher @matcher ||= Regexp.new symbols.join('|') end def self.symbol?(candidate) - /^(#{matcher})$/.match?(candidate) + candidate =~ /^(#{matcher})$/ end def self.get(identifier) return identifier if identifier.is_a?(HeadMusic::Sign) all.detect do |sign| @@ -32,18 +35,21 @@ end end def self.by(key, value) all.detect do |sign| - if %i[cents semitones].include?(key.to_sym) - sign.send(key) == value - end + sign.send(key) == value if %i[cents semitones].include?(key.to_sym) end end + def name + identifier.to_s.tr('_', ' ') + end + def representions - [identifier, identifier.to_s, name, ascii, unicode, html_entity].reject { |representation| representation.to_s.strip == '' } + [identifier, identifier.to_s, name, ascii, unicode, html_entity]. + reject { |representation| representation.to_s.strip == '' } end def semitones cents / 100.0 end @@ -52,17 +58,16 @@ unicode end def <=>(other) other = HeadMusic::Sign.get(other) - self.cents <=> other.cents + cents <=> other.cents end private def initialize(attributes) @identifier = attributes[:identifier] - @name = attributes[:name] @ascii = attributes[:ascii] @unicode = attributes[:unicode] @html_entity = attributes[:html_entity] @cents = attributes[:cents] end