Class: HeadMusic::Rudiment::Alteration

Inherits:
Object
  • Object
show all
Includes:
Comparable
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Alteration (private)

Returns a new instance of Alteration.



102
103
104
105
106
# File 'lib/head_music/rudiment/alteration.rb', line 102

def initialize(attributes)
  @identifier = attributes[:identifier]
  @cents = attributes[:cents]
  initialize_musical_symbols(attributes[:symbols])
end

Instance Attribute Details

#centsObject (readonly)

Returns the value of attribute cents.



11
12
13
# File 'lib/head_music/rudiment/alteration.rb', line 11

def cents
  @cents
end

#identifierObject (readonly)

Returns the value of attribute identifier.



11
12
13
# File 'lib/head_music/rudiment/alteration.rb', line 11

def identifier
  @identifier
end

#musical_symbolsObject (readonly)

Returns the value of attribute musical_symbols.



11
12
13
# File 'lib/head_music/rudiment/alteration.rb', line 11

def musical_symbols
  @musical_symbols
end

Class Method Details

.allObject



40
41
42
# File 'lib/head_music/rudiment/alteration.rb', line 40

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

.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

.matcherObject



48
49
50
# File 'lib/head_music/rudiment/alteration.rb', line 48

def self.matcher
  @matcher ||= Regexp.new symbols.join("|")
end

.symbol?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/head_music/rudiment/alteration.rb', line 52

def self.symbol?(candidate)
  candidate =~ /^(#{matcher})$/
end

.symbolsObject



44
45
46
# File 'lib/head_music/rudiment/alteration.rb', line 44

def self.symbols
  @symbols ||= all.map { |alteration| [alteration.ascii, alteration.unicode] }.flatten.reject { |s| s.nil? || s.empty? }
end

Instance Method Details

#<=>(other) ⇒ Object



91
92
93
94
# File 'lib/head_music/rudiment/alteration.rb', line 91

def <=>(other)
  other = HeadMusic::Rudiment::Alteration.get(other)
  cents <=> other.cents
end

#initialize_musical_symbols(list) ⇒ Object (private)



108
109
110
111
112
113
114
115
116
# File 'lib/head_music/rudiment/alteration.rb', line 108

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

#musical_symbolObject



96
97
98
# File 'lib/head_music/rudiment/alteration.rb', line 96

def musical_symbol
  musical_symbols.first
end

#nameObject



70
71
72
# File 'lib/head_music/rudiment/alteration.rb', line 70

def name
  identifier.to_s.tr("_", " ")
end

#representionsObject



74
75
76
77
# File 'lib/head_music/rudiment/alteration.rb', line 74

def representions
  [identifier, identifier.to_s, name, ascii, unicode, html_entity]
    .reject { |representation| representation.to_s.strip == "" }
end

#semitonesObject



79
80
81
# File 'lib/head_music/rudiment/alteration.rb', line 79

def semitones
  cents / 100.0
end

#to_sObject



87
88
89
# File 'lib/head_music/rudiment/alteration.rb', line 87

def to_s
  unicode
end