Class: RelatonBib::LocalizedString

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

Overview

Localized string.

Direct Known Subclasses

Forename, FormattedString

Constant Summary

Constants included from RelatonBib

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RelatonBib

array, format_date, grammar_hash, parse_date, parse_yaml

Methods included from Config

#configuration, #configure

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
# 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) && content.none?
    raise ArgumentError, "LocalizedString content is empty"
  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|
                 case c
                 when Hash
                   LocalizedString.new c[:content], c[:language], c[:script]
                 when String then LocalizedString.new c
                 else c
                 end
               end
             else cleanup 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

#==(other) ⇒ Object



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

def ==(other)
  return false unless other.is_a? LocalizedString

  content == other.content && language == other.language && script == other.script
end

#cleanup(str) ⇒ String

Should be implemented in subclass.

Parameters:

  • str (String)

    content

Returns:

  • (String)

    cleaned content



145
146
147
# File 'lib/relaton_bib/localized_string.rb', line 145

def cleanup(str)
  str
end

#empty?Boolean

Returns true if content is empty.

Returns:

  • (Boolean)


60
61
62
# File 'lib/relaton_bib/localized_string.rb', line 60

def empty?
  content.empty?
end

#encode(cnt) ⇒ String

Encode content.

Parameters:

  • cnt (String)

    content

Returns:

  • (String)

    encoded content



84
85
86
# File 'lib/relaton_bib/localized_string.rb', line 84

def encode(cnt)
  escp cnt
end

#escp(str) ⇒ String

Escope HTML entities.

Parameters:

  • str (String)

    input string

Returns:

  • (String)

    output string



95
96
97
98
99
100
# File 'lib/relaton_bib/localized_string.rb', line 95

def escp(str)
  return unless str

  coder = HTMLEntities.new
  coder.encode coder.decode(str.dup.force_encoding("UTF-8"))
end

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

Parameters:

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

    number of elements

Returns:

  • (String)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/relaton_bib/localized_string.rb', line 119

def to_asciibib(prefix = "", count = 1, has_attrs = false) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity,Metrics/MethodLength
  pref = prefix.empty? ? prefix : "#{prefix}."
  case content
  when String
    unless language&.any? || script&.any? || has_attrs
      return "#{prefix}:: #{content}\n"
    end

    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
  when Array
    content.map { |c| c.to_asciibib "#{pref}variant", content.size }.join
  else count > 1 ? "#{prefix}::\n" : ""
  end
end

#to_hashHash+

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/relaton_bib/localized_string.rb', line 103

def to_hash # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  if content.nil? || content.is_a?(String)
    # return content unless language || script

    hash = {}
    hash["content"] = content # unless content.nil? || content.empty?
    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

String representation.

Returns:

  • (String)


51
52
53
# File 'lib/relaton_bib/localized_string.rb', line 51

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

#to_xml(builder) ⇒ Object

Parameters:

  • builder (Nokogiri::XML::Builder)


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

def to_xml(builder) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  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.parent.inner_html = encode content
  end
end