Module: HeadMusic::Named

Overview

NameRudiment is a module to be included in classes whose instances may be identified by name. Locale and LocalizedName, the value objects it builds names from, live in their own files.

Defined Under Namespace

Modules: ClassMethods Classes: Locale, LocalizedName

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alias_name_keysObject (readonly)

Returns the value of attribute alias_name_keys.



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

def alias_name_keys
  @alias_name_keys
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



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

def name_key
  @name_key
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object



38
39
40
41
42
# File 'lib/head_music/named.rb', line 38

def ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil)
  @localized_names ||= []
  @localized_names << LocalizedName.new(name: name, locale_code: locale_code, abbreviation: abbreviation)
  @localized_names.uniq!
end

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



30
31
32
33
34
35
36
# File 'lib/head_music/named.rb', line 30

def localized_name(locale_code: Locale::DEFAULT_CODE)
  locale = Locale.get(locale_code || Locale::DEFAULT_CODE)
  localized_name_in_matching_locale(locale) ||
    localized_name_in_locale_matching_language(locale) ||
    localized_name_in_default_locale ||
    localized_names.first
end

#localized_name_in_default_localeObject (private)



59
60
61
# File 'lib/head_music/named.rb', line 59

def localized_name_in_default_locale
  localized_names.detect { |name| name.locale_code == Locale::DEFAULT_CODE }
end

#localized_name_in_locale_matching_language(locale) ⇒ Object (private)



55
56
57
# File 'lib/head_music/named.rb', line 55

def localized_name_in_locale_matching_language(locale)
  localized_names.detect { |candidate| candidate.language == locale.language }
end

#localized_name_in_matching_locale(locale) ⇒ Object (private)



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

def localized_name_in_matching_locale(locale)
  localized_names.detect { |candidate| candidate.locale_code == locale.code }
end

#localized_namesObject

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



45
46
47
# File 'lib/head_music/named.rb', line 45

def localized_names
  @localized_names ||= []
end

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



26
27
28
# File 'lib/head_music/named.rb', line 26

def name(locale_code: Locale::DEFAULT_CODE)
  localized_name(locale_code: locale_code)&.name
end

#name=(name) ⇒ Object



22
23
24
# File 'lib/head_music/named.rb', line 22

def name=(name)
  ensure_localized_name(name: name)
end