Class: Opencdd::Languages

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/opencdd/languages.rb', line 16

def source
  @source
end

#translationsObject (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

#allObject



24
25
26
# File 'lib/opencdd/languages.rb', line 24

def all
  [@source] + @translations
end

#empty?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/opencdd/languages.rb', line 32

def empty?
  @source.nil? || @source.empty?
end

#hashObject



50
51
52
# File 'lib/opencdd/languages.rb', line 50

def hash
  [source, translations].hash
end

#include?(lang) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/opencdd/languages.rb', line 28

def include?(lang)
  all.include?(lang.to_s)
end

#sizeObject



36
37
38
# File 'lib/opencdd/languages.rb', line 36

def size
  all.size
end

#to_aObject



40
41
42
# File 'lib/opencdd/languages.rb', line 40

def to_a
  all
end