Class: HeadMusic::Alteration
- Inherits:
-
Object
- Object
- HeadMusic::Alteration
- Includes:
- Comparable
- Defined in:
- lib/head_music/alteration.rb
Overview
A Alteration 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
- ALTERATION_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
- ALTERATION_IDENTIFIERS =
ALTERATION_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.
8 9 10 |
# File 'lib/head_music/alteration.rb', line 8 def cents @cents end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
8 9 10 |
# File 'lib/head_music/alteration.rb', line 8 def identifier @identifier end |
#musical_symbols ⇒ Object (readonly)
Returns the value of attribute musical_symbols.
8 9 10 |
# File 'lib/head_music/alteration.rb', line 8 def musical_symbols @musical_symbols end |
Class Method Details
.all ⇒ Object
37 38 39 |
# File 'lib/head_music/alteration.rb', line 37 def self.all ALTERATION_RECORDS.map { |attributes| new(attributes) } end |
.by(key, value) ⇒ Object
61 62 63 64 65 |
# File 'lib/head_music/alteration.rb', line 61 def self.by(key, value) all.detect do |alteration| alteration.send(key) == value if %i[cents semitones].include?(key.to_sym) end end |
.get(identifier) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/head_music/alteration.rb', line 53 def self.get(identifier) return identifier if identifier.is_a?(HeadMusic::Alteration) all.detect do |alteration| alteration.representions.include?(identifier) end end |
.matcher ⇒ Object
45 46 47 |
# File 'lib/head_music/alteration.rb', line 45 def self.matcher @matcher ||= Regexp.new symbols.join("|") end |
.symbol?(candidate) ⇒ Boolean
49 50 51 |
# File 'lib/head_music/alteration.rb', line 49 def self.symbol?(candidate) candidate =~ /^(#{matcher})$/ end |
.symbols ⇒ Object
41 42 43 |
# File 'lib/head_music/alteration.rb', line 41 def self.symbols @symbols ||= all.map { |alteration| [alteration.ascii, alteration.unicode] }.flatten.reject { |s| s.nil? || s.empty? } end |
Instance Method Details
#<=>(other) ⇒ Object
88 89 90 91 |
# File 'lib/head_music/alteration.rb', line 88 def <=>(other) other = HeadMusic::Alteration.get(other) cents <=> other.cents end |
#musical_symbol ⇒ Object
93 94 95 |
# File 'lib/head_music/alteration.rb', line 93 def musical_symbol musical_symbols.first end |
#name ⇒ Object
67 68 69 |
# File 'lib/head_music/alteration.rb', line 67 def name identifier.to_s.tr("_", " ") end |
#representions ⇒ Object
71 72 73 74 |
# File 'lib/head_music/alteration.rb', line 71 def representions [identifier, identifier.to_s, name, ascii, unicode, html_entity] .reject { |representation| representation.to_s.strip == "" } end |
#semitones ⇒ Object
76 77 78 |
# File 'lib/head_music/alteration.rb', line 76 def semitones cents / 100.0 end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/head_music/alteration.rb', line 84 def to_s unicode end |