Class: HeadMusic::Content::Role

Inherits:
Object
  • Object
show all
Includes:
Named
Defined in:
lib/head_music/content/role.rb

Overview

The part a person played. Roles are grouped by the level they attach to, so the model cannot record a publisher as having composed the music.

Constant Summary collapse

KEYS_BY_LEVEL =
{
  work: %i[composer songwriter lyricist librettist],
  project: %i[arranger transcriber orchestrator reconstructor],
  publication: %i[author editor engraver publisher]
}.freeze
KEYS =
KEYS_BY_LEVEL.values.flatten.freeze
LEVELS =
KEYS_BY_LEVEL.keys.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Role (private)

Returns a new instance of Role.



90
91
92
93
# File 'lib/head_music/content/role.rb', line 90

def initialize(key)
  @key = key
  @name_key = key
end

Instance Attribute Details

#alias_name_keys ⇒ Object (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#key ⇒ Object (readonly)

Returns the value of attribute key.



61
62
63
# File 'lib/head_music/content/role.rb', line 61

def key
  @key
end

#name_key ⇒ Object (readonly) Originally defined in module Named

Returns the value of attribute name_key.

Class Method Details

.all ⇒ Object



37
38
39
# File 'lib/head_music/content/role.rb', line 37

def self.all
  KEYS.map { |key| get(key) }
end

.for_level(level) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.for_level(level)
  keys = KEYS_BY_LEVEL[level&.to_sym]
  raise ArgumentError, "unknown credit level: #{level.inspect}" if keys.nil?

  keys.map { |key| get(key) }
end

.get(identifier) ⇒ Object

Unlike Named's .get_by_name, an unrecognized identifier raises rather than minting a role: the twelve roles are the whole vocabulary.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
# File 'lib/head_music/content/role.rb', line 20

def self.get(identifier)
  return identifier if identifier.is_a?(self)

  key = key_for(identifier)
  raise ArgumentError, "unknown credit role: #{identifier.inspect}" if key.nil?

  @instances ||= {}
  @instances[key] ||= new(key)
end

.key_for(identifier) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/head_music/content/role.rb', line 41

def self.key_for(identifier)
  return nil if identifier.nil?

  key = HeadMusic::Utilities::HashKey.for(identifier)
  return key if KEYS.include?(key)

  key_for_translated_name(identifier.to_s.strip)
end

.key_for_translated_name(name) ⇒ Object (private)



50
51
52
53
54
55
56
57
58
# File 'lib/head_music/content/role.rb', line 50

def self.key_for_translated_name(name)
  return nil if name.empty?

  KEYS.detect do |key|
    I18n.config.available_locales.any? do |locale_code|
      I18n.translate(key, scope: "head_music.credit_roles", locale: locale_code).casecmp?(name)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
74
75
76
# File 'lib/head_music/content/role.rb', line 71

def ==(other)
  return eql?(other) if other.is_a?(self.class)
  return false unless other.is_a?(Symbol) || other.is_a?(String)

  key == self.class.key_for(other)
end

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  other.is_a?(self.class) && key == other.key
end

#hash ⇒ Object



82
83
84
# File 'lib/head_music/content/role.rb', line 82

def hash
  [self.class, key].hash
end

#level ⇒ Object



63
64
65
# File 'lib/head_music/content/role.rb', line 63

def level
  @level ||= KEYS_BY_LEVEL.detect { |_level, keys| keys.include?(key) }.first
end

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

#localized_name_in_default_locale ⇒ Object (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_names ⇒ Object Originally defined in module Named

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

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



67
68
69
# File 'lib/head_music/content/role.rb', line 67

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

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