Sha256: 0050f55ec43d7f6ab3cb28c171be7a8e1c155f73829acefbf70f1253bc213e0f

Contents?: true

Size: 1.11 KB

Versions: 4

Compression:

Stored size: 1.11 KB

Contents

module ApiResource

  module Finders

    class SingleFinder < AbstractFinder

      def load
        return nil if self.response.blank?

        @loaded = true

        @internal_object = self.klass.instantiate_record(self.response)
        # 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

        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"].blank?
          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

4 entries across 4 versions & 1 rubygems

Version Path
api_resource-0.6.25 lib/api_resource/finders/single_finder.rb
api_resource-0.6.24 lib/api_resource/finders/single_finder.rb
api_resource-0.6.23 lib/api_resource/finders/single_finder.rb
api_resource-0.6.22 lib/api_resource/finders/single_finder.rb