Class: ApiResource::Finders::SingleObjectAssociationFinder
- Inherits:
-
AbstractFinder
- Object
- AbstractFinder
- ApiResource::Finders::SingleObjectAssociationFinder
- Defined in:
- lib/api_resource/finders/single_object_association_finder.rb
Instance Attribute Summary
Attributes inherited from AbstractFinder
#condition, #found, #internal_object, #klass
Instance Method Summary collapse
-
#initialize(klass, condition, internal_object = nil) ⇒ SingleObjectAssociationFinder
constructor
A new instance of SingleObjectAssociationFinder.
-
#load ⇒ Object
since it is only a single object we can just load from the service_uri and deal with includes.
Methods inherited from AbstractFinder
#all, #headers, #instance_of?, #is_a?, #kind_of?, #method_missing, #offset, #paginated?, #response, #total_entries
Constructor Details
#initialize(klass, condition, internal_object = nil) ⇒ SingleObjectAssociationFinder
Returns a new instance of SingleObjectAssociationFinder.
7 8 9 10 |
# File 'lib/api_resource/finders/single_object_association_finder.rb', line 7 def initialize(klass, condition, internal_object = nil) super(klass, condition) @internal_object = internal_object end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ApiResource::Finders::AbstractFinder
Instance Method Details
#load ⇒ Object
since it is only a single object we can just load from the service_uri and deal with includes
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 |
# File 'lib/api_resource/finders/single_object_association_finder.rb', line 14 def load # otherwise just instantiate the record unless self.condition.remote_path raise "Tried to load association without a remote path" end # check our response return nil if self.response.blank? # get our internal object @internal_object ||= begin if self.response.is_a?(Array) self.klass.instantiate_record(self.response.first) else self.klass.instantiate_record(self.response) end end # mark us as loaded @loaded = true # now that the object is loaded, resolve the includes id_hash = self.condition.included_objects.inject({}) do |hash, assoc| hash[assoc] = Array.wrap( @internal_object.send( @internal_object.class.association_foreign_key_field(assoc) ) ) hash end # apply our includes included_objects = self.load_includes(id_hash) self.apply_includes(@internal_object, included_objects) return @internal_object end |