Sha256: 608ba9c180c5c1d186e9b3aa9f7a0053d0b9dce65a094aeecda224cdf3257e77

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module Arx

  # Entity/model representing an arXiv paper's category.
  class Category
    include HappyMapper
    include Inspector

    # The attributes of an arXiv paper's category.
    ATTRIBUTES = %i[name full_name]

    tag 'category'

    # @!method name
    # The abbreviated name of the category.
    #
    # @return [String]
    attribute :name, Cleaner, parser: :clean, tag: 'term'

    # The full name of the category.
    #
    # @see CATEGORIES
    # @return [String]
    def full_name
      CATEGORIES[name]
    end

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

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

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

    # Equality check against another category.
    #
    # @param category [Category] The category to compare against.
    # @return [Boolean]
    def ==(category)
      if category.is_a? Category
        name == category.name
      else
        false
      end
    end

    # A string representation of the {Category} object.
    #
    # @return [String]
    def to_s
      "Arx::Category(name: #{name}, full_name: #{full_name || 'nil'})"
    end

    inspector *ATTRIBUTES
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arx-1.3.0 lib/arx/entities/category.rb