Sha256: b5b0abcde93404bd13bd9a6f3ba8d3085bbf34e36540ff23ea75f063a133918b

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module RelatonBib
  # Localized string.
  class LocalizedString
    # @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
    end

    # @return [Hash]
    def to_hash
      hash = { content: content }
      hash[:language] = language if language&.any?
      hash[:script] = script if script&.any?
      hash
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relaton-bib-0.3.4 lib/relaton_bib/localized_string.rb