class Mangadex::Relationship

Constants

Attributes

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

Public Class Methods

attributes_to_inspect() click to toggle source
# File lib/mangadex/relationship.rb, line 59
def self.attributes_to_inspect
  [:id, :type, :related]
end
from_data(data, source_obj = nil) click to toggle source

data: Relationship data source_obj: The object to witch the object belongs to

# File lib/mangadex/relationship.rb, line 27
def from_data(data, source_obj = nil)
  data = data.transform_keys(&:to_s)
  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 Class Methods

class_for_relationship_type(type) click to toggle source
# File lib/mangadex/relationship.rb, line 48
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

Public Instance Methods

method_missing(value) click to toggle source
# File lib/mangadex/relationship.rb, line 64
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