Sha256: d8f577372457af4175502eec26641bcce68b21289c503ace6c4399ec6410d40f

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

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

    class << self
      def from_data(data)
        data = data.with_indifferent_access
        klass = class_for_relationship_type(data['type'])

        return klass.from_data(data) if klass && data['attributes']&.any?

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

      private

      def build_attributes(data)
        klass = class_for_relationship_type(data['type'])
        if klass.present?
          klass.from_data(data)
        else
          OpenStruct.new(data['attributes'])
        end
      end

      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 inspect
      "#<#{self.class} id=#{id.inspect} type=#{type.inspect}>"
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mangadex-5.3.2 lib/mangadex/relationship.rb
mangadex-5.3.1.3 lib/mangadex/relationship.rb
mangadex-5.3.1.2 lib/mangadex/relationship.rb
mangadex-5.3.1.1 lib/mangadex/relationship.rb
mangadex-5.3.1 lib/mangadex/relationship.rb
mangadex-5.3.0 lib/mangadex/relationship.rb