Class: Pobo::DTO::LocalizedString

Inherits:
Object
  • Object
show all
Defined in:
lib/pobo/dto/localized_string.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_value = nil, translations: {}) ⇒ LocalizedString

Returns a new instance of LocalizedString.



8
9
10
11
# File 'lib/pobo/dto/localized_string.rb', line 8

def initialize(default_value = nil, translations: {})
  @translations = translations.transform_keys(&:to_s)
  @translations["default"] = default_value if default_value
end

Instance Attribute Details

#translations ⇒ Object (readonly)

Returns the value of attribute translations.



6
7
8
# File 'lib/pobo/dto/localized_string.rb', line 6

def translations
  @translations
end

Class Method Details

.create(default_value) ⇒ Object



13
14
15
# File 'lib/pobo/dto/localized_string.rb', line 13

def self.create(default_value)
  new(default_value)
end

.from_hash(hash) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/pobo/dto/localized_string.rb', line 17

def self.from_hash(hash)
  return nil if hash.nil?

  instance = new
  hash.each do |key, value|
    instance.translations[key.to_s] = value
  end
  instance
end

Instance Method Details

#default ⇒ Object



33
34
35
# File 'lib/pobo/dto/localized_string.rb', line 33

def default
  @translations["default"]
end

#get(language) ⇒ Object



37
38
39
# File 'lib/pobo/dto/localized_string.rb', line 37

def get(language)
  @translations[language.to_s]
end

#to_hash ⇒ Object Also known as: to_h



41
42
43
# File 'lib/pobo/dto/localized_string.rb', line 41

def to_hash
  @translations.dup
end

#with_translation(language, value) ⇒ Object



27
28
29
30
31
# File 'lib/pobo/dto/localized_string.rb', line 27

def with_translation(language, value)
  new_translations = @translations.dup
  new_translations[language.to_s] = value
  self.class.new(nil, translations: new_translations)
end