Class: HeadMusic::Rudiment::Clef

Inherits:
Base
  • Object
show all
Includes:
Named
Defined in:
lib/head_music/rudiment/clef.rb

Overview

A clef assigns pitches to the lines and spaces of a staff.

Constant Summary collapse

RECORDS =
YAML.load_file(File.expand_path("clefs.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Clef (private)

Returns a new instance of Clef.



60
61
62
63
# File 'lib/head_music/rudiment/clef.rb', line 60

def initialize(name)
  record = record_for_name(name)
  initialize_data_from_record(record)
end

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#lineObject (readonly)

Returns the value of attribute line.



16
17
18
# File 'lib/head_music/rudiment/clef.rb', line 16

def line
  @line
end

#musical_symbolsObject (readonly)

Returns the value of attribute musical_symbols.



16
17
18
# File 'lib/head_music/rudiment/clef.rb', line 16

def musical_symbols
  @musical_symbols
end

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

#pitchObject (readonly)

Returns the value of attribute pitch.



16
17
18
# File 'lib/head_music/rudiment/clef.rb', line 16

def pitch
  @pitch
end

Class Method Details

.get(name) ⇒ Object



12
13
14
# File 'lib/head_music/rudiment/clef.rb', line 12

def self.get(name)
  get_by_name(name)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  HeadMusic::Utilities::HashKey.for(self) == HeadMusic::Utilities::HashKey.for(other)
end

#clef_typeObject



24
25
26
# File 'lib/head_music/rudiment/clef.rb', line 24

def clef_type
  "#{pitch.letter_name}-clef"
end

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#initialize_data_from_record(record) ⇒ Object (private)



86
87
88
89
90
91
92
# File 'lib/head_music/rudiment/clef.rb', line 86

def initialize_data_from_record(record)
  initialize_keys_from_record(record)
  @pitch = HeadMusic::Rudiment::Pitch.get(record[:pitch])
  @line = record[:line]
  @modern = record[:modern]
  initialize_musical_symbols(record[:symbols])
end

#initialize_keys_from_record(record) ⇒ Object (private)



94
95
96
97
# File 'lib/head_music/rudiment/clef.rb', line 94

def initialize_keys_from_record(record)
  @name_key = record[:name_key]
  @alias_name_keys = [record[:alias_name_keys]].flatten.compact
end

#initialize_musical_symbols(list) ⇒ Object (private)



99
100
101
102
103
# File 'lib/head_music/rudiment/clef.rb', line 99

def initialize_musical_symbols(list)
  @musical_symbols = (list || []).map do |symbol_data|
    HeadMusic::Rudiment::MusicalSymbol.new(**symbol_data.slice(:ascii, :html_entity, :unicode))
  end
end

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (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_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#modern?Boolean

Returns:

  • (Boolean)


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

def modern?
  @modern
end

#musical_symbolObject



20
21
22
# File 'lib/head_music/rudiment/clef.rb', line 20

def musical_symbol
  musical_symbols.first
end

#name(locale_code: Locale::DEFAULT_CODE) ⇒ Object



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

def name(locale_code: Locale::DEFAULT_CODE)
  I18n.translate(name_key, scope: "head_music.clefs", locale: locale_code)
end

#name=(name) ⇒ Object Originally defined in module Named

#name_key_translations(name_keys) ⇒ Object (private)



78
79
80
81
82
83
84
# File 'lib/head_music/rudiment/clef.rb', line 78

def name_key_translations(name_keys)
  name_keys.map do |name_key|
    I18n.config.available_locales.map do |locale_code|
      I18n.translate(name_key, scope: "head_music.clefs", locale: locale_code)
    end.flatten.uniq.compact
  end.flatten.uniq.compact
end

#name_keys_from_record(record) ⇒ Object (private)



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

def name_keys_from_record(record)
  ([record[:name_key]] + [record[:alias_name_keys]]).flatten.compact.uniq.map(&:to_sym)
end

#pitch_for_line(line_number) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/head_music/rudiment/clef.rb', line 28

def pitch_for_line(line_number)
  @line_pitches ||= {}
  @line_pitches[line_number] ||= begin
    steps = (line_number - line) * 2
    pitch.natural_steps(steps)
  end
end

#pitch_for_space(space_number) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/head_music/rudiment/clef.rb', line 36

def pitch_for_space(space_number)
  @space_pitches ||= {}
  @space_pitches[space_number] ||= begin
    steps = (space_number - line) * 2 + 1
    pitch.natural_steps(steps)
  end
end

#record_for_name(name) ⇒ Object (private)



65
66
67
68
69
70
71
72
# File 'lib/head_music/rudiment/clef.rb', line 65

def record_for_name(name)
  name = name.to_s.strip
  key = HeadMusic::Utilities::HashKey.for(name)
  RECORDS.detect do |record|
    name_keys = name_keys_from_record(record)
    name_keys.include?(key) || name_key_translations(name_keys).include?(name)
  end
end