Sha256: 31ce3112ddbf2fd61e16eda0d67d87463c24c6d77bd49888f122f306f1931677

Contents?: true

Size: 1.18 KB

Versions: 22

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require "relaton_bib/localized_string"

module RelatonBib
  # Formatted string
  class FormattedString < LocalizedString
    FORMATS = %w[text/plain text/html application/docbook+xml
                 application/tei+xml text/x-asciidoc text/markdown
                 application/x-isodoc+xml].freeze

    # @return [String]
    attr_reader :format

    # @param content [String]
    # @param language [String, NilClass] language code Iso639
    # @param script [String, NilClass] script code Iso15924
    # @param format [String] the content type
    def initialize(content:, language: nil, script: nil, format: "text/plain")
      # if format && !FORMATS.include?(format)
      #   raise ArgumentError, %{Format "#{format}" is invalid.}
      # end

      super(content, language, script)
      @format = format
    end

    # @param builder [Nokogiri::XML::Builder]
    def to_xml(builder)
      builder.parent["format"] = format if format
      super
    end

    # @return [Hash]
    def to_hash
      hash = super
      return hash unless format

      hash = { "content" => hash } if hash.is_a? String
      hash["format"] = format if format
      hash
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
relaton-bib-1.0.3 lib/relaton_bib/formatted_string.rb
relaton-bib-1.0.2 lib/relaton_bib/formatted_string.rb
relaton-bib-1.0.1 lib/relaton_bib/formatted_string.rb
relaton-bib-1.0.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.9.2 lib/relaton_bib/formatted_string.rb
relaton-bib-0.9.1 lib/relaton_bib/formatted_string.rb
relaton-bib-0.9.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.8.1 lib/relaton_bib/formatted_string.rb
relaton-bib-0.8.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.7.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.6.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.5.2 lib/relaton_bib/formatted_string.rb
relaton-bib-0.5.1 lib/relaton_bib/formatted_string.rb
relaton-bib-0.5.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.4.1 lib/relaton_bib/formatted_string.rb
relaton-bib-0.4.0 lib/relaton_bib/formatted_string.rb
relaton-bib-0.3.12 lib/relaton_bib/formatted_string.rb
relaton-bib-0.3.11 lib/relaton_bib/formatted_string.rb
relaton-bib-0.3.10 lib/relaton_bib/formatted_string.rb
relaton-bib-0.3.9 lib/relaton_bib/formatted_string.rb