Sha256: 3e826e197594daded929ad0f13911d1ea3bf926f23175593e0e17c9ce3c92428

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 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
      def from_data(data)
        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'])
        end

        new(
          id: data['id'],
          type: data['type'],
          attributes: OpenStruct.new(data['attributes']),
          related: data['related'],
        )
      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

6 entries across 6 versions & 1 rubygems

Version Path
mangadex-5.4.9 lib/mangadex/relationship.rb
mangadex-5.3.3.4 lib/mangadex/relationship.rb
mangadex-5.3.3.3 lib/mangadex/relationship.rb
mangadex-5.3.3.2 lib/mangadex/relationship.rb
mangadex-5.3.3.1 lib/mangadex/relationship.rb
mangadex-5.3.3 lib/mangadex/relationship.rb