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

Returns a new instance of LocalizedString.

Parameters:

  • content (String, Array<RelatonBib::LocalizedString>)
  • language (String) (defaults to: nil)

    language code Iso639

  • script (String) (defaults to: nil)

    script code Iso15924



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/relaton_bib/localized_string.rb', line 20

def initialize(content, language = nil, script = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  if content.is_a? Array
    inv = content.reject { |c| c.is_a?(LocalizedString) || c.is_a?(Hash) }
  end
  unless content.is_a?(String) || inv&.none? && content.any?
    klass = content.is_a?(Array) ? inv.first.class : content.class
    raise ArgumentError, "invalid LocalizedString content type: #{klass}"
  end
  @language = language.is_a?(String) ? [language] : language
  @script = script.is_a?(String) ? [script] : script
  @content = if content.is_a?(Array)
               content.map do |c|
                 if c.is_a?(Hash)
                   LocalizedString.new c[:content], c[:language], c[:script]
                 else c
                 end
               end
             else content
             end
end

Instance Attribute Details

#contentString, Array<RelatonBib::LocalizedString>

Returns:



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

def content
  @content
end

#languageArray<String> (readonly)

Returns language Iso639 code.

Returns:

  • (Array<String>)

    language Iso639 code



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

def language
  @language
end

#scriptArray<String> (readonly)

Returns script Iso15924 code.

Returns:

  • (Array<String>)

    script Iso15924 code



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

def script
  @script
end

Instance Method Details

#empty?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


47
48
49
# File 'lib/relaton_bib/localized_string.rb', line 47

def empty?
  content.empty?
end

#to_asciibib(prefix = "", count = 1) ⇒ String

Parameters:

  • prefix (String) (defaults to: "")
  • count (Integer) (defaults to: 1)

    number of elements

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/relaton_bib/localized_string.rb', line 80

def to_asciibib(prefix = "", count = 1) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  pref = prefix.empty? ? prefix : prefix + "."
  if content.is_a? String
    out = count > 1 ? "#{prefix}::\n" : ""
    out += "#{pref}content:: #{content}\n"
    language&.each { |l| out += "#{pref}language:: #{l}\n" }
    script&.each { |s| out += "#{pref}script:: #{s}\n" }
    out
  else
    content.map { |c| c.to_asciibib "#{pref}variant", content.size }.join
  end
end

#to_hashHash

Returns:

  • (Hash)


65
66
67
68
69
70
71
72
73
74
75
# File 'lib/relaton_bib/localized_string.rb', line 65

def to_hash # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  if content.is_a? String
    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
  else content.map &:to_hash
  end
end

#to_sString

Returns:

  • (String)


42
43
44
# File 'lib/relaton_bib/localized_string.rb', line 42

def to_s
  content.is_a?(String) ? content : content.first.to_s
end

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


52
53
54
55
56
57
58
59
60
61
62
# File 'lib/relaton_bib/localized_string.rb', line 52

def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
  return unless content

  if content.is_a?(Array)
    content.each { |c| builder.variant { c.to_xml builder } }
  else
    builder.parent["language"] = language.join(",") if language&.any?
    builder.parent["script"]   = script.join(",") if script&.any?
    builder.text content.encode(xml: :text)
  end
end