Class: HeadMusic::Sign
- Inherits:
-
Object
- Object
- HeadMusic::Sign
- Includes:
- Comparable
- Defined in:
- lib/head_music/sign.rb
Overview
A Sign is a symbol that modifies pitch, such as a sharp, flat, or natural. In French, sharps and flats in the key signature are called “altérations”.
Constant Summary collapse
- 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_RECORDS.map { |attributes| attributes[:identifier] }.freeze
Instance Attribute Summary collapse
-
#cents ⇒ Object
readonly
Returns the value of attribute cents.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#musical_symbols ⇒ Object
readonly
Returns the value of attribute musical_symbols.
Class Method Summary collapse
- .all ⇒ Object
- .by(key, value) ⇒ Object
- .get(identifier) ⇒ Object
- .matcher ⇒ Object
- .symbol?(candidate) ⇒ Boolean
- .symbols ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #musical_symbol ⇒ Object
- #name ⇒ Object
- #representions ⇒ Object
- #semitones ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#cents ⇒ Object (readonly)
Returns the value of attribute cents.
10 11 12 |
# File 'lib/head_music/sign.rb', line 10 def cents @cents end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
10 11 12 |
# File 'lib/head_music/sign.rb', line 10 def identifier @identifier end |
#musical_symbols ⇒ Object (readonly)
Returns the value of attribute musical_symbols.
10 11 12 |
# File 'lib/head_music/sign.rb', line 10 def musical_symbols @musical_symbols end |
Class Method Details
.all ⇒ Object
39 40 41 |
# File 'lib/head_music/sign.rb', line 39 def self.all SIGN_RECORDS.map { |attributes| new(attributes) } end |
.by(key, value) ⇒ Object
63 64 65 66 67 |
# File 'lib/head_music/sign.rb', line 63 def self.by(key, value) all.detect do |sign| sign.send(key) == value if %i[cents semitones].include?(key.to_sym) end end |
.get(identifier) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/head_music/sign.rb', line 55 def self.get(identifier) return identifier if identifier.is_a?(HeadMusic::Sign) all.detect do |sign| sign.representions.include?(identifier) end end |
.matcher ⇒ Object
47 48 49 |
# File 'lib/head_music/sign.rb', line 47 def self.matcher @matcher ||= Regexp.new symbols.join("|") end |
.symbol?(candidate) ⇒ Boolean
51 52 53 |
# File 'lib/head_music/sign.rb', line 51 def self.symbol?(candidate) candidate =~ /^(#{matcher})$/ end |
.symbols ⇒ Object
43 44 45 |
# File 'lib/head_music/sign.rb', line 43 def self.symbols @symbols ||= all.map { |sign| [sign.ascii, sign.unicode] }.flatten.reject { |s| s.nil? || s.empty? } end |
Instance Method Details
#<=>(other) ⇒ Object
90 91 92 93 |
# File 'lib/head_music/sign.rb', line 90 def <=>(other) other = HeadMusic::Sign.get(other) cents <=> other.cents end |
#musical_symbol ⇒ Object
95 96 97 |
# File 'lib/head_music/sign.rb', line 95 def musical_symbol musical_symbols.first end |
#name ⇒ Object
69 70 71 |
# File 'lib/head_music/sign.rb', line 69 def name identifier.to_s.tr("_", " ") end |
#representions ⇒ Object
73 74 75 76 |
# File 'lib/head_music/sign.rb', line 73 def representions [identifier, identifier.to_s, name, ascii, unicode, html_entity] .reject { |representation| representation.to_s.strip == "" } end |
#semitones ⇒ Object
78 79 80 |
# File 'lib/head_music/sign.rb', line 78 def semitones cents / 100.0 end |
#to_s ⇒ Object
86 87 88 |
# File 'lib/head_music/sign.rb', line 86 def to_s unicode end |