load (DataMapper::Model)

    # File lib/dm-core/model.rb, line 458
        def load(records, query)
          repository      = query.repository
          repository_name = repository.name
          fields          = query.fields
          discriminator   = properties(repository_name).discriminator
          no_reload       = !query.reload?

          field_map = fields.map { |property| [ property, property.field ] }.to_hash

          records.map do |record|
            identity_map = nil
            key_values   = nil
            resource     = nil

            case record
              when Hash
                # remap fields to use the Property object
                record = record.dup
                field_map.each { |property, field| record[property] = record.delete(field) if record.key?(field) }

                model = discriminator && record[discriminator] || self

                resource = if (key_values = record.values_at(*model.key(repository_name))).all?
                  identity_map = repository.identity_map(model)
                  identity_map[key_values]
                end

                resource ||= model.allocate

                fields.each do |property|
                  next if no_reload && property.loaded?(resource)

                  value = record[property]

                  # TODO: typecasting should happen inside the Adapter
                  # and all values should come back as expected objects
                  if property.custom?
                    value = property.type.load(value, property)
                  end

                  property.set!(resource, value)
                end

              when Resource
                model = record.model

                resource = if (key_values = record.key).all?
                  identity_map = repository.identity_map(model)
                  identity_map[key_values]
                end

                resource ||= model.allocate

                fields.each do |property|
                  next if no_reload && property.loaded?(resource)

                  property.set!(resource, property.get!(record))
                end
            end

            resource.instance_variable_set(:@repository, repository)
            resource.instance_variable_set(:@saved,      true)

            if identity_map
              # defer setting the IdentityMap so second level caches can
              # record the state of the resource after loaded
              identity_map[key_values] = resource
            else
              resource.freeze
            end

            resource
          end
        end