Sha256: 8096e07c7064febd080d418b750d05ad00b78d8dafa3ca81753dd1890887489c

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

# typed: false
require_relative "internal/with_attributes"

module Mangadex
  class MangadexObject
    extend T::Sig
    include Internal::WithAttributes

    def self.attributes_to_inspect
      to_inspect = [:id, :type]
      if self.respond_to?(:inspect_attributes)
        to_inspect.concat(Array(self.inspect_attributes))
      end

      to_inspect
    end

    def initialize(**args)
      args.keys.each do |attribute|
        original_attribute = attribute
        attribute = Mangadex::Utils.underscore(attribute.to_s)
        attribute_to_set = "#{attribute}="

        if respond_to?(attribute_to_set)
          if %w(created_at updated_at publish_at).include?(attribute)
            args[original_attribute] = DateTime.parse(args[original_attribute])
          end

          send(attribute_to_set, args[original_attribute])
        else
          warn("Ignoring setter `#{attribute_to_set}` on #{self.class.name}...")
        end
      end

      self.type = self.class.type if self.type.blank?
    end

    def eq?(other)
      return id == other.id if respond_to?(:id) && other.respond_to?(:id)

      super
    end

    def hash
      id.hash
    end

    def inspect
      string = "#<#{self.class.name}:#{self.object_id} "
      fields = self.class.attributes_to_inspect.map do |field|
        value = self.send(field)
        if !value.nil?
          "@#{field}=\"#{value}\""
        end
      rescue => error
        "@#{field}[!]={#{error.class.name}: #{error.message}}"
      end.compact
      string << fields.join(" ") << ">"
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
mangadex-5.10.0 lib/mangadex/mangadex_object.rb
mangadex-5.9.0 lib/mangadex/mangadex_object.rb
mangadex-5.8.0 lib/mangadex/mangadex_object.rb
mangadex-5.7.5.3 lib/mangadex/mangadex_object.rb
mangadex-5.7.5.2 lib/mangadex/mangadex_object.rb
mangadex-5.7.5.1 lib/mangadex/mangadex_object.rb
mangadex-5.7.5 lib/mangadex/mangadex_object.rb
mangadex-5.5.8 lib/mangadex/mangadex_object.rb
mangadex-5.5.6 lib/mangadex/mangadex_object.rb
mangadex-5.4.16 lib/mangadex/mangadex_object.rb