Class: Mangadex::Relationship

Inherits:
MangadexObject show all
Defined in:
lib/mangadex/relationship.rb

Constant Summary collapse

%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

Instance Attribute Summary collapse

Attributes included from Internal::WithAttributes

#related_type, #relationships

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MangadexObject

#eq?, #hash, #initialize, #inspect

Methods included from Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

This class inherits a constructor from Mangadex::MangadexObject

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(value) ⇒ Object



64
65
66
67
68
69
70
71
# 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

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/mangadex/relationship.rb', line 4

def attributes
  @attributes
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/mangadex/relationship.rb', line 4

def id
  @id
end

Returns the value of attribute related.



4
5
6
# File 'lib/mangadex/relationship.rb', line 4

def related
  @related
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/mangadex/relationship.rb', line 4

def type
  @type
end

Class Method Details

.attributes_to_inspectObject



59
60
61
# File 'lib/mangadex/relationship.rb', line 59

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

.from_data(data, source_obj = nil) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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