Class: CocinaDisplay::Language
- Inherits:
-
Object
- Object
- CocinaDisplay::Language
- Defined in:
- lib/cocina_display/language.rb
Overview
A language associated with part or all of a Cocina object.
Constant Summary collapse
- SEARCHWORKS_LANGUAGES_FILE_PATH =
File.join(__dir__, "..", "..", "config", "searchworks_languages.yml").freeze
Instance Attribute Summary collapse
-
#cocina ⇒ Object
readonly
Returns the value of attribute cocina.
Class Method Summary collapse
-
.searchworks_languages ⇒ Hash{String => String}
A hash of language codes to language names recognized by Searchworks.
Instance Method Summary collapse
-
#code ⇒ String?
The language code, e.g.
-
#decoded_value ⇒ String?
Decoded name of the language based on the code, if present.
-
#initialize(cocina) ⇒ Language
constructor
Create a Language object from Cocina structured data.
-
#label ⇒ String
Display label for this field.
-
#searchworks_language? ⇒ Boolean
True if the language is recognized by Searchworks.
-
#to_s ⇒ String?
The language name for display.
Constructor Details
#initialize(cocina) ⇒ Language
Create a Language object from Cocina structured data.
18 19 20 |
# File 'lib/cocina_display/language.rb', line 18 def initialize(cocina) @cocina = cocina end |
Instance Attribute Details
#cocina ⇒ Object (readonly)
Returns the value of attribute cocina.
8 9 10 |
# File 'lib/cocina_display/language.rb', line 8 def cocina @cocina end |
Class Method Details
.searchworks_languages ⇒ Hash{String => String}
A hash of language codes to language names recognized by Searchworks.
12 13 14 |
# File 'lib/cocina_display/language.rb', line 12 def self.searchworks_languages @searchworks_languages ||= YAML.safe_load_file(SEARCHWORKS_LANGUAGES_FILE_PATH) end |
Instance Method Details
#code ⇒ String?
The language code, e.g. an ISO 639 code like “eng” or “spa”.
30 31 32 |
# File 'lib/cocina_display/language.rb', line 30 def code cocina["code"] end |
#decoded_value ⇒ String?
Decoded name of the language based on the code, if present.
36 37 38 |
# File 'lib/cocina_display/language.rb', line 36 def decoded_value Language.searchworks_languages[code] if searchworks_language? end |
#label ⇒ String
Display label for this field.
42 43 44 |
# File 'lib/cocina_display/language.rb', line 42 def label cocina["displayLabel"].presence || I18n.t("cocina_display.field_label.language") end |
#searchworks_language? ⇒ Boolean
True if the language is recognized by Searchworks.
49 50 51 |
# File 'lib/cocina_display/language.rb', line 49 def searchworks_language? Language.searchworks_languages.value?(cocina["value"]) || Language.searchworks_languages.key?(code) end |
#to_s ⇒ String?
The language name for display.
24 25 26 |
# File 'lib/cocina_display/language.rb', line 24 def to_s cocina["value"] || decoded_value end |