Module: JSONAPIonify::Api::Resource::Defaults::RequestContexts

Extended by:
ActiveSupport::Concern
Defined in:
lib/jsonapionify/api/resource/defaults/request_contexts.rb

Instance Method Summary collapse

Instance Method Details

#find_instance(item, pointer:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/jsonapionify/api/resource/defaults/request_contexts.rb', line 115

def find_instance(item, pointer:)
  should_error = false
  resource     = find_resource(item, pointer: pointer)
  unless (instance = resource.find_instance item[:id])
    should_error = true
    error :resource_invalid do
      self.pointer pointer
      self.detail "could not find resource: `#{item[:type]}` with id: #{item[:id]}"
    end
  end
  halt if should_error
  instance
end

#find_instances(items, pointer:) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jsonapionify/api/resource/defaults/request_contexts.rb', line 102

def find_instances(items, pointer:)
  should_error = false
  instances    = items.map.each_with_index do |item, i|
    begin
      find_instance item, pointer: "#{pointer}/#{i}"
    rescue Errors::RequestError
      should_error = true
    end
  end
  halt if should_error
  instances
end

#find_resource(item, pointer:) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/jsonapionify/api/resource/defaults/request_contexts.rb', line 129

def find_resource(item, pointer:)
  should_error = false
  unless (resource = self.class.api.resource item[:type])
    should_error = true
    error :resource_invalid do
      self.pointer pointer
      self.detail "could not find resource: `#{item[:type]}`"
    end
  end
  halt if should_error
  resource
end