Class: Opencdd::Languages
- Inherits:
-
Object
- Object
- Opencdd::Languages
- Defined in:
- lib/opencdd/languages.rb
Overview
Value object representing the language configuration of a CDD
dictionary. The source language is the one the data was originally
authored in; translation languages are additional languages that
appear in the .xls property keys (e.g. MDC_P004.de).
The exporter scans each entity's @properties hash for
<property_id>.<lang> keys to build per-field language maps
(TODO.work/08). This class is the model object that aggregates
"what languages does this dictionary have?" — used by the browser
to render a language switcher and by the data pipeline to report
language coverage.
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#translations ⇒ Object
readonly
Returns the value of attribute translations.
Class Method Summary collapse
-
.from_properties(properties, default_source: "en") ⇒ Object
Scan a properties hash for
<property_id>.<lang>keys and return a Languages object covering every language seen.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #all ⇒ Object
- #empty? ⇒ Boolean
- #hash ⇒ Object
- #include?(lang) ⇒ Boolean
-
#initialize(source: "en", translations: []) ⇒ Languages
constructor
A new instance of Languages.
- #size ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(source: "en", translations: []) ⇒ Languages
Returns a new instance of Languages.
18 19 20 21 22 |
# File 'lib/opencdd/languages.rb', line 18 def initialize(source: "en", translations: []) @source = source.to_s @translations = Array(translations).map(&:to_s).uniq - [@source] freeze end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
16 17 18 |
# File 'lib/opencdd/languages.rb', line 16 def source @source end |
#translations ⇒ Object (readonly)
Returns the value of attribute translations.
16 17 18 |
# File 'lib/opencdd/languages.rb', line 16 def translations @translations end |
Class Method Details
.from_properties(properties, default_source: "en") ⇒ Object
Scan a properties hash for <property_id>.<lang> keys and
return a Languages object covering every language seen. The
source language defaults to default_source when no explicit
source-language key is present.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/opencdd/languages.rb', line 58 def self.from_properties(properties, default_source: "en") langs = properties.keys.each_with_object(Set.new) do |key, acc| next unless key.include?(".") prefix, lang = key.split(".", 2) next unless lang =~ /\A[a-z]{2}(-[a-z0-9]+)?\z/i acc << lang end source = langs.include?(default_source) ? default_source : (langs.first || default_source) translations = langs.to_a - [source] new(source: source, translations: translations) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
44 45 46 47 |
# File 'lib/opencdd/languages.rb', line 44 def ==(other) other.is_a?(Opencdd::Languages) && source == other.source && translations == other.translations end |
#all ⇒ Object
24 25 26 |
# File 'lib/opencdd/languages.rb', line 24 def all [@source] + @translations end |
#empty? ⇒ Boolean
32 33 34 |
# File 'lib/opencdd/languages.rb', line 32 def empty? @source.nil? || @source.empty? end |
#hash ⇒ Object
50 51 52 |
# File 'lib/opencdd/languages.rb', line 50 def hash [source, translations].hash end |
#include?(lang) ⇒ Boolean
28 29 30 |
# File 'lib/opencdd/languages.rb', line 28 def include?(lang) all.include?(lang.to_s) end |
#size ⇒ Object
36 37 38 |
# File 'lib/opencdd/languages.rb', line 36 def size all.size end |
#to_a ⇒ Object
40 41 42 |
# File 'lib/opencdd/languages.rb', line 40 def to_a all end |