Sha256: 105ee662b9058f038ac17219430dcd381d98721b8bf47ef916a5b0bfc08d6a1b

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# typed: false
module Mangadex
  class Relationship < MangadexObject
    attr_accessor :id, :type, :related, :attributes

    RELATED_VALUES = %w(
      monochrome
      main_story
      adapted_from
      based_on
      prequel
      side_story
      doujinshi
      same_franchise
      shared_universe
      sequel
      spin_off
      alternate_story
      preserialization
      colored
      serialization
    ).freeze

    class << self
      # data: Relationship data
      # source_obj: The object to witch the object belongs to
      def from_data(data, source_obj = nil)
        data = data.with_indifferent_access
        klass = class_for_relationship_type(data['type'])

        if klass && data['attributes']&.any?
          return klass.from_data(data, related_type: data['related'], source_obj: source_obj)
        end

        relationships = [source_obj] if source_obj

        new(
          id: data['id'],
          type: data['type'],
          attributes: OpenStruct.new(data['attributes']),
          related: data['related'],
          relationships: relationships,
        )
      end

      private

      def class_for_relationship_type(type)
        module_parts = self.name.split('::')
        module_name = module_parts.take(module_parts.size - 1).join('::')
        klass_name = "#{module_name}::#{type.split('_').collect(&:capitalize).join}"

        return unless Object.const_defined?(klass_name)

        Object.const_get(klass_name)
      end
    end

    def self.attributes_to_inspect
      [:id, :type, :related]
    end

    
    def method_missing(value)
      return super unless value.end_with?("?")

      looking_for_related = value.to_s.split("?").first
      return super unless RELATED_VALUES.include?(looking_for_related)

      !related.nil? && related == looking_for_related
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mangadex-5.4.11.3 lib/mangadex/relationship.rb
mangadex-5.4.11.2 lib/mangadex/relationship.rb
mangadex-5.4.11.1 lib/mangadex/relationship.rb
mangadex-5.4.11 lib/mangadex/relationship.rb