Sha256: 662699d781641ef8d497f9401036b8542fda52c4532183f3946bf910cb549b5f

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module RelatonBib
  class TypedTitleString
    TITLE_TYPES = %w[alternative original unofficial subtitle main].freeze

    # @return [String]
    attr_reader :type

    # @return [RelatonBib::FormattedString]
    attr_reader :title

    # rubocop:disable Metrics/AbcSize, Metrics/MethodLength

    # @param type [String]
    # @param title [RelatonBib::FormattedString, Hash]
    # @param content [String]
    # @param language [String]
    # @param script [String]
    def initialize(**args)
      if args[:type] && !TITLE_TYPES.include?(args[:type])
        raise ArgumentError, %{The type #{args[:type]} is invalid.}
      end

      unless args[:title] || args[:content]
        raise ArgumentError, %{Keyword "title" or "content" should be passed.}
      end

      @type = args[:type]

      if args[:title]
        @title = args[:title]
      else
        fsargs = args.select do |k, _v|
          %i[content language script format].include? k 
        end
        @title = FormattedString.new(fsargs)
      end
    end
    # rubocop:enable Metrics/AbcSize, Metrics/MethodLength

    # @param builder [Nokogiri::XML::Builder]
    def to_xml(builder)
      builder.parent[:type] = type if type
      title.to_xml builder
    end

    # @return [Hash]
    def to_hash
      hash = {}
      hash[:type] = type if type
      hash.merge title.to_hash
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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