Class: ApiResource::Finders::ResourceFinder

Inherits:
AbstractFinder show all
Defined in:
lib/api_resource/finders/resource_finder.rb

Instance Attribute Summary

Attributes inherited from AbstractFinder

#condition, #found, #internal_object, #klass

Instance Method Summary collapse

Methods inherited from AbstractFinder

#all, #headers, #initialize, #instance_of?, #is_a?, #kind_of?, #method_missing, #offset, #paginated?, #response, #total_entries

Constructor Details

This class inherits a constructor from ApiResource::Finders::AbstractFinder

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ApiResource::Finders::AbstractFinder

Instance Method Details

#loadObject

this is a little bit simpler, it's always a collection and does not require a remote path



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/api_resource/finders/resource_finder.rb', line 9

def load
  begin
    return [] if self.response.blank?

    @loaded = true

    if self.response.is_a?(Array)
      @internal_object = self.klass.instantiate_collection(
        self.response
      )
    else
      @internal_object = [
        self.klass.instantiate_record(self.response)
      ]
    end

    id_hash = self.condition.included_objects.inject({}) do |accum, assoc|
      accum[assoc] = @internal_object.collect do |obj|
        obj.send(self.klass.association_foreign_key_field(assoc))
      end
      accum[assoc].flatten!
      accum[assoc].uniq!
      accum
    end
    included_objects = self.load_includes(id_hash)

    self.apply_includes(@internal_object, included_objects)


    # Removed to mirror ActiveRecord
    # e.g. LifebookerClient::Provider.find([1])
    #
    # # looks hacky, but we want to return only a single
    # # object in case of a find call.
    # if @internal_object.count == 1 && self.build_load_path =~ /(&|\?)find/
    #   @internal_object = @internal_object.first
    # end

    return @internal_object
  rescue ApiResource::ResourceNotFound
    nil
  end
  @internal_object
end