Class: HeadMusic::Sign

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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#centsObject (readonly)

Returns the value of attribute cents.



10
11
12
# File 'lib/head_music/sign.rb', line 10

def cents
  @cents
end

#identifierObject (readonly)

Returns the value of attribute identifier.



10
11
12
# File 'lib/head_music/sign.rb', line 10

def identifier
  @identifier
end

#musical_symbolsObject (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

.allObject



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

.matcherObject



47
48
49
# File 'lib/head_music/sign.rb', line 47

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

.symbol?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/head_music/sign.rb', line 51

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

.symbolsObject



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_symbolObject



95
96
97
# File 'lib/head_music/sign.rb', line 95

def musical_symbol
  musical_symbols.first
end

#nameObject



69
70
71
# File 'lib/head_music/sign.rb', line 69

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

#representionsObject



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

#semitonesObject



78
79
80
# File 'lib/head_music/sign.rb', line 78

def semitones
  cents / 100.0
end

#to_sObject



86
87
88
# File 'lib/head_music/sign.rb', line 86

def to_s
  unicode
end