Sha256: 5e8c3c8b5b50a491f15292bfa6f3edddc82836332fa0e6a848f4b1d358699046

Contents?: true

Size: 1.37 KB

Versions: 12

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RelatonBib
  # Localized string.
  class LocalizedString
    include RelatonBib

    # @return [Array<String>] language Iso639 code
    attr_reader :language

    # @return [Array<String>] script Iso15924 code
    attr_reader :script

    # @return [String]
    attr_accessor :content

    # @param content [String]
    # @param language [String] language code Iso639
    # @param script [String] script code Iso15924
    def initialize(content, language = nil, script = nil)
      @language = language.is_a?(String) ? [language] : language
      @script = script.is_a?(String) ? [script] : script
      @content = content
    end

    # @return [String]
    def to_s
      content
    end

    # @return [TrueClass, FalseClass]
    def empty?
      content.empty?
    end

    # @param builder [Nokogiri::XML::Builder]
    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

    # @return [Hash]
    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
relaton-bib-1.0.3 lib/relaton_bib/localized_string.rb
relaton-bib-1.0.2 lib/relaton_bib/localized_string.rb
relaton-bib-1.0.1 lib/relaton_bib/localized_string.rb
relaton-bib-1.0.0 lib/relaton_bib/localized_string.rb
relaton-bib-0.9.2 lib/relaton_bib/localized_string.rb
relaton-bib-0.9.1 lib/relaton_bib/localized_string.rb
relaton-bib-0.9.0 lib/relaton_bib/localized_string.rb
relaton-bib-0.8.1 lib/relaton_bib/localized_string.rb
relaton-bib-0.8.0 lib/relaton_bib/localized_string.rb
relaton-bib-0.7.0 lib/relaton_bib/localized_string.rb
relaton-bib-0.6.0 lib/relaton_bib/localized_string.rb
relaton-bib-0.5.2 lib/relaton_bib/localized_string.rb