module Mangadex::Internal::WithAttributes

Constants

USING_ATTRIBUTES

Attributes

attributes[RW]
id[RW]
relationships[RW]
type[RW]

Public Instance Methods

==(other) click to toggle source
# File lib/mangadex/internal/with_attributes.rb, line 81
def ==(other)
  if other.is_a?(String)
    id == other
  elsif other.respond_to?(:id)
    id == other.id
  else
    false
  end
end
any_relationships?() click to toggle source
# File lib/mangadex/internal/with_attributes.rb, line 91
def any_relationships?
  Array(relationships).any?
end
from_data(data, related_type: nil, source_obj: nil) click to toggle source
# File lib/mangadex/internal/with_attributes.rb, line 32
        def from_data(data, related_type: nil, source_obj: nil)
          base_class_name = self.name.gsub('::', '_')
          klass_name = self.name
          target_attributes_class_name = "#{base_class_name}_Attributes"

          data = data.transform_keys(&:to_s)

          klass = if const_defined?(target_attributes_class_name)
            Object.const_get(target_attributes_class_name)
          else
            class_contents = <<-END
              # typed: true
              class ::#{target_attributes_class_name} < MangadexObject
                #{USING_ATTRIBUTES[klass_name].map {|attribute| "sig { returns(T.untyped) }; attr_accessor(:#{attribute})"}.join(';')}

                def self.attributes_to_inspect
                  #{USING_ATTRIBUTES[klass_name]}
                end
              end
            END

            eval class_contents
            Object.const_get(target_attributes_class_name)
          end

          relationships = data['relationships']&.map do |relationship_data|
            Relationship.from_data(relationship_data, MangadexObject.new(**data))
          end

          found_attributes = data['attributes'] || {}
          attributes = klass.new(**Hash(found_attributes.symbolize_keys))

          initialize_hash = {
            id: data['id'],
            type: data['type'] || self.type,
            attributes: attributes,
            related_type: related_type,
          }

          relationships = [source_obj].compact unless relationships.present?
          initialize_hash.merge!({relationships: relationships}) if relationships.present?

          # binding.pry

          new(**initialize_hash)
        end
has_attributes(*attributes) click to toggle source
# File lib/mangadex/internal/with_attributes.rb, line 18
def has_attributes(*attributes)
  USING_ATTRIBUTES[self.name] = Array(USING_ATTRIBUTES[self.name]).concat(
    attributes.map(&:to_sym)
  )
end
method_missing(method_name, *args, **kwargs) click to toggle source
Calls superclass method
# File lib/mangadex/internal/with_attributes.rb, line 95
def method_missing(method_name, *args, **kwargs)
  if self.class.attributes.include?(method_name.to_sym)
    return if attributes.nil?
    return unless attributes.respond_to?(method_name)

    attributes.send(method_name)
  elsif any_relationships?
    existing_relationships = relationships.map(&:type)
    looking_for_relationship = method_name.to_s

    if existing_relationships.include?(looking_for_relationship)
      result = relationships.select do |relationship|
        relationship.type == looking_for_relationship
      end
      result.size == 1 ? result.first : result
    else
      super
    end
  elsif !related_type.nil?
    return super unless method_name.end_with?("?")

    looking_for_related = method_name.to_s.split("?").first
    return super unless Mangadex::Relationship::RELATED_VALUES.include?(looking_for_related)

    related_type == looking_for_related
  else
    super
  end
end