lib/arx/entities/author.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Arx

  # Entity/model representing an arXiv paper's author.
  class Author
    include HappyMapper
    include Inspector

    # The attributes of an arXiv paper's author.
    ATTRIBUTES = %i[name affiliated? affiliations]

    tag 'author'

    # @!method name
    # The name of the author.
    #
    # @return [String]
    element :name, Cleaner, tag: 'name', parser: :clean

    # @!method affiliations
    # The author's affiliations.
    #
    # @return [Array<String>]
    has_many :affiliations, Cleaner, tag: 'affiliation', parser: :clean

    # Whether or not the author has any affiliations.
    #
    # @return [Boolean]
    def affiliated?
      !affiliations.empty?
    end

    # Serializes the {Author} object into a +Hash+.
    #
    # @return [Hash]
    def to_h
      Hash[*ATTRIBUTES.map {|_| [_, send(_)]}.flatten(1)]
    end

    # Serializes the {Author} object into a valid JSON hash.
    #
    # @return [Hash] The resulting JSON hash.
    def as_json
      JSON.parse to_json
    end

    # Serializes the {Author} object into a valid JSON string.
    #
    # @return [String] The resulting JSON string.
    def to_json
      to_h.to_json
    end

    # Equality check against another author.
    #
    # @note This only performs a basic equality check between the authors' names.
    # @param author [Author] The author to compare against.
    # @return [Boolean]
    def ==(author)
      if author.is_a? Author
        name == author.name
      else
        false
      end
    end

    # A string representation of the {Author} object.
    #
    # @return [String]
    def to_s
      "Arx::Author(name: #{name}, affiliations: [#{affiliations.join(', ')}])"
    end

    inspector *ATTRIBUTES
  end
end