Class: HeadMusic::Rudiment::Alteration
- Includes:
- Comparable, Named
- Defined in:
- lib/head_music/rudiment/alteration.rb
Overview
An 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
- SYMBOLS =
ALTERATION_RECORDS.map { |attributes| attributes[:symbols].map { |symbol| [symbol[:unicode], symbol[:ascii]] } }.flatten.freeze
- PATTERN =
Regexp.union(SYMBOLS.reject { |s| s.nil? || s.empty? })
- MATCHER =
PATTERN
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#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.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
Class Method Summary collapse
- .all ⇒ Object
- .by(key, value) ⇒ Object
- .from_pitched_item(input) ⇒ Object
- .get(identifier) ⇒ Object
- .get_by_name(name) ⇒ Object
- .symbol?(candidate) ⇒ Boolean
- .symbols ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object included from Named
-
#initialize(attributes) ⇒ Alteration
constructor
private
A new instance of Alteration.
- #initialize_localized_names ⇒ Object private
- #initialize_musical_symbols(list) ⇒ Object private
- #localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object included from Named
- #localized_name_in_default_locale ⇒ Object included from Named private
- #localized_name_in_locale_matching_language(locale) ⇒ Object included from Named private
- #localized_name_in_matching_locale(locale) ⇒ Object included from Named private
-
#localized_names ⇒ Object
included
from Named
Returns an array of LocalizedName instances that are synonymous with the name.
- #musical_symbol ⇒ Object
- #name(locale_code: I18n.locale) ⇒ Object
- #name=(name) ⇒ Object included from Named
- #representions ⇒ Object
- #semitones ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Alteration (private)
Returns a new instance of Alteration.
110 111 112 113 114 115 |
# File 'lib/head_music/rudiment/alteration.rb', line 110 def initialize(attributes) @identifier = attributes[:identifier] @cents = attributes[:cents] initialize_musical_symbols(attributes[:symbols]) initialize_localized_names end |
Instance Attribute Details
#alias_name_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#cents ⇒ Object (readonly)
Returns the value of attribute cents.
12 13 14 |
# File 'lib/head_music/rudiment/alteration.rb', line 12 def cents @cents end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
12 13 14 |
# File 'lib/head_music/rudiment/alteration.rb', line 12 def identifier @identifier end |
#musical_symbols ⇒ Object (readonly)
Returns the value of attribute musical_symbols.
12 13 14 |
# File 'lib/head_music/rudiment/alteration.rb', line 12 def musical_symbols @musical_symbols end |
#name_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
Class Method Details
.all ⇒ Object
44 45 46 |
# File 'lib/head_music/rudiment/alteration.rb', line 44 def self.all ALTERATION_RECORDS.map { |attributes| new(attributes) } end |
.by(key, value) ⇒ Object
64 65 66 67 68 |
# File 'lib/head_music/rudiment/alteration.rb', line 64 def self.by(key, value) all.detect do |alteration| alteration.send(key) == value if %i[cents semitones].include?(key.to_sym) end end |
.from_pitched_item(input) ⇒ Object
74 75 76 |
# File 'lib/head_music/rudiment/alteration.rb', line 74 def self.from_pitched_item(input) nil end |
.get(identifier) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/head_music/rudiment/alteration.rb', line 56 def self.get(identifier) return identifier if identifier.is_a?(HeadMusic::Rudiment::Alteration) all.detect do |alteration| alteration.representions.include?(identifier) end end |
.get_by_name(name) ⇒ Object
70 71 72 |
# File 'lib/head_music/rudiment/alteration.rb', line 70 def self.get_by_name(name) all.detect { |alteration| alteration.name == name.to_s } end |
.symbol?(candidate) ⇒ Boolean
52 53 54 |
# File 'lib/head_music/rudiment/alteration.rb', line 52 def self.symbol?(candidate) SYMBOLS.include?(candidate) end |
.symbols ⇒ Object
48 49 50 |
# File 'lib/head_music/rudiment/alteration.rb', line 48 def self.symbols @symbols ||= all.map { |alteration| [alteration.ascii, alteration.unicode] }.flatten.reject { |s| s.nil? || s.empty? } end |
Instance Method Details
#<=>(other) ⇒ Object
99 100 101 102 |
# File 'lib/head_music/rudiment/alteration.rb', line 99 def <=>(other) other = HeadMusic::Rudiment::Alteration.get(other) cents <=> other.cents end |
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named
#initialize_localized_names ⇒ Object (private)
117 118 119 120 121 |
# File 'lib/head_music/rudiment/alteration.rb', line 117 def initialize_localized_names # Initialize default English names ensure_localized_name(name: identifier.to_s.tr("_", " "), locale_code: :en) # Additional localized names will be loaded from locale files end |
#initialize_musical_symbols(list) ⇒ Object (private)
123 124 125 126 127 128 129 130 131 |
# File 'lib/head_music/rudiment/alteration.rb', line 123 def initialize_musical_symbols(list) @musical_symbols = (list || []).map do |record| HeadMusic::Rudiment::MusicalSymbol.new( unicode: record[:unicode], ascii: record[:ascii], html_entity: record[:html_entity] ) end end |
#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named
#localized_name_in_default_locale ⇒ Object (private) Originally defined in module Named
#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named
#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named
#localized_names ⇒ Object Originally defined in module Named
Returns an array of LocalizedName instances that are synonymous with the name.
#musical_symbol ⇒ Object
104 105 106 |
# File 'lib/head_music/rudiment/alteration.rb', line 104 def musical_symbol musical_symbols.first end |
#name(locale_code: I18n.locale) ⇒ Object
78 79 80 |
# File 'lib/head_music/rudiment/alteration.rb', line 78 def name(locale_code: I18n.locale) super || identifier.to_s.tr("_", " ") end |
#name=(name) ⇒ Object Originally defined in module Named
#representions ⇒ Object
82 83 84 85 |
# File 'lib/head_music/rudiment/alteration.rb', line 82 def representions [identifier, identifier.to_s, name, ascii, unicode, html_entity] .reject { |representation| representation.to_s.strip == "" } end |
#semitones ⇒ Object
87 88 89 |
# File 'lib/head_music/rudiment/alteration.rb', line 87 def semitones cents / 100.0 end |
#to_s ⇒ Object
95 96 97 |
# File 'lib/head_music/rudiment/alteration.rb', line 95 def to_s unicode end |