Sha256: 953c31f411dd32a2bdb3add177d2d70932f427e79ce735d56d88920b382007de

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module ApiResource

	module Finders

		class SingleFinder < AbstractFinder

			def load
				data = self.klass.connection.get(self.build_load_path)
				@loaded = true
				return nil if data.blank?
				@internal_object = self.klass.instantiate_record(data)
				# now that the object is loaded, resolve the includes
				id_hash = self.condition.included_objects.inject({}) do |accum, assoc|
					accum[assoc] = Array.wrap(
						@internal_object.send(
							@internal_object.class.association_foreign_key_field(assoc)
						)
					)
					accum
				end

				included_objects = self.load_includes(id_hash)

				self.apply_includes(@internal_object, included_objects)

				return @internal_object
			end

			protected

			def build_load_path
				if self.condition.to_hash["id"].nil?
					raise "Invalid evaluation of a SingleFinder without an ID"
				end
				args = self.condition.to_hash
				id = args.delete("id")
				prefix_opts, query_opts = self.klass.split_options(args)
	      self.klass.element_path(id, prefix_opts, query_opts)
	    end

		end

	end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
api_resource-0.6.6 lib/api_resource/finders/single_finder.rb
api_resource-0.6.5 lib/api_resource/finders/single_finder.rb
api_resource-0.6.4 lib/api_resource/finders/single_finder.rb
api_resource-0.6.3 lib/api_resource/finders/single_finder.rb
api_resource-0.6.2 lib/api_resource/finders/single_finder.rb
api_resource-0.6.1 lib/api_resource/finders/single_finder.rb