module HeadMusic::Named

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

Attributes

alias_name_keys[R]
name_key[R]

Public Class Methods

included(base) click to toggle source
# File lib/head_music/named.rb, line 62
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) click to toggle source
# File lib/head_music/named.rb, line 84
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) click to toggle source
# File lib/head_music/named.rb, line 76
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_names() click to toggle source

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

# File lib/head_music/named.rb, line 91
def localized_names
  @localized_names ||= []
end
name(locale_code: Locale::DEFAULT_CODE) click to toggle source
# File lib/head_music/named.rb, line 72
def name(locale_code: Locale::DEFAULT_CODE)
  localized_name(locale_code: locale_code)&.name
end
name=(name) click to toggle source
# File lib/head_music/named.rb, line 68
def name=(name)
  ensure_localized_name(name: name)
end

Private Instance Methods

hash_key() click to toggle source
# File lib/head_music/named.rb, line 109
def hash_key
  HeadMusic::Utilities::HashKey.for(name)
end
localized_name_in_default_locale() click to toggle source
# File lib/head_music/named.rb, line 105
def localized_name_in_default_locale
  localized_names.detect { |name| name.locale_code == Locale::DEFAULT_CODE }
end
localized_name_in_locale_matching_language(locale) click to toggle source
# File lib/head_music/named.rb, line 101
def localized_name_in_locale_matching_language(locale)
  localized_names.detect { |candidate| candidate.language == locale.language }
end
localized_name_in_matching_locale(locale) click to toggle source
# File lib/head_music/named.rb, line 97
def localized_name_in_matching_locale(locale)
  localized_names.detect { |candidate| candidate.locale_code == locale.code }
end