Module: HeadMusic::Named

Overview

NameRudiment is a module to be included in classes whose instances may be identified by name.

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.



64
65
66
# File 'lib/head_music/named.rb', line 64

def alias_name_keys
  @alias_name_keys
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



64
65
66
# File 'lib/head_music/named.rb', line 64

def name_key
  @name_key
end

Class Method Details

.included(base) ⇒ Object



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

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

Instance Method Details

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



82
83
84
85
86
# File 'lib/head_music/named.rb', line 82

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



74
75
76
77
78
79
80
# File 'lib/head_music/named.rb', line 74

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)



103
104
105
# File 'lib/head_music/named.rb', line 103

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)



99
100
101
# File 'lib/head_music/named.rb', line 99

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)



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

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.



89
90
91
# File 'lib/head_music/named.rb', line 89

def localized_names
  @localized_names ||= []
end

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



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

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

#name=(name) ⇒ Object



66
67
68
# File 'lib/head_music/named.rb', line 66

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