Class: JsonAPIObjectMapper::Parser::IncludedResources

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/jsonapi-object-mapper/parser/included_resources.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(included_resources = []) ⇒ IncludedResources

Returns a new instance of IncludedResources.



16
17
18
19
20
21
22
23
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 16

def initialize(included_resources = [])
  included_resources ||= []
  @resource = included_resources.each_with_object({}) do |include, hash|
    hash[format_key(include)] = include
  end.freeze

  freeze
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



8
9
10
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 8

def resource
  @resource
end

Class Method Details

.load(included_resources) ⇒ Object



11
12
13
14
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 11

def self.load(included_resources)
  return included_resources if included_resources.is_a?(self)
  new included_resources
end

Instance Method Details

#fetch(relationship, fallback = nil) ⇒ Object



25
26
27
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 25

def fetch(relationship, fallback = nil)
  @resource.fetch(format_key(relationship), fallback || relationship)
end

#format_key(relationship) ⇒ Object



33
34
35
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 33

def format_key(relationship)
  "#{relationship['type']}:#{relationship['id']}"
end

#included?(relationship) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/jsonapi-object-mapper/parser/included_resources.rb', line 29

def included?(relationship)
  @resource.key?(format_key(relationship))
end