Class: RelatonBib::LocalizedString

Inherits:
Object
  • Object
show all
Includes:
RelatonBib
Defined in:
lib/relaton_bib/localized_string.rb

Overview

Localized string.

Direct Known Subclasses

FormattedString

Constant Summary

Constants included from RelatonBib

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RelatonBib

parse_date

Constructor Details

#initialize(content, language = nil, script = nil) ⇒ LocalizedString



20
21
22
23
24
# File 'lib/relaton_bib/localized_string.rb', line 20

def initialize(content, language = nil, script = nil)
  @language = language.is_a?(String) ? [language] : language
  @script = script.is_a?(String) ? [script] : script
  @content = content
end

Instance Attribute Details

#contentString



15
16
17
# File 'lib/relaton_bib/localized_string.rb', line 15

def content
  @content
end

#languageArray<String> (readonly)



9
10
11
# File 'lib/relaton_bib/localized_string.rb', line 9

def language
  @language
end

#scriptArray<String> (readonly)



12
13
14
# File 'lib/relaton_bib/localized_string.rb', line 12

def script
  @script
end

Instance Method Details

#empty?TrueClass, FalseClass



32
33
34
# File 'lib/relaton_bib/localized_string.rb', line 32

def empty?
  content.empty?
end

#to_hashHash



46
47
48
49
50
51
52
53
# File 'lib/relaton_bib/localized_string.rb', line 46

def to_hash
  return content unless language || script

  hash = { "content" => content }
  hash["language"] = single_element_array(language) if language&.any?
  hash["script"] = single_element_array(script) if script&.any?
  hash
end

#to_sString



27
28
29
# File 'lib/relaton_bib/localized_string.rb', line 27

def to_s
  content
end

#to_xml(builder) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/relaton_bib/localized_string.rb', line 37

def to_xml(builder)
  return unless content

  builder.parent["language"] = language.join(",") if language&.any?
  builder.parent["script"]   = script.join(",") if script&.any?
  builder.text content.encode(xml: :text)
end