Sha256: a5fad619f9313557620f516a34b42b0f468a15b345fd696e3f35938a76f56a9f

Contents?: true

Size: 1.01 KB

Versions: 16

Compression:

Stored size: 1.01 KB

Contents

# Module containing methods responsible for searching ActiveRepository objects
module ActiveRepository #:nodoc:
  module Finders #:nodoc:
    # Searches for an object containing the id in #id
    def find(id)
      begin
        if repository?
          super(id)
        else
          serialize!(PersistenceAdapter.find(self, id))
        end
      rescue Exception => e
        message = "Couldn't find #{self} with ID=#{id}"
        message = "Couldn't find all #{self} objects with IDs (#{id.join(', ')})" if id.is_a?(Array)

        raise ActiveHash::RecordNotFound.new(message)
      end
    end

    # Returns first persisted object
    def first
      repository? ? super : get(:first)
    end

    # Returns last persisted object
    def last
      repository? ? super : get(:last)
    end

    private
    # Returns the object in the position specified in #position
    def get(position)
      object = PersistenceAdapter.send(position, self)
      object.present? ? serialize!(object.attributes) : nil
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
active_repository-0.5.1 lib/active_repository/finders.rb
active_repository-0.5.0 lib/active_repository/finders.rb
active_repository-0.4.3 lib/active_repository/finders.rb
active_repository-0.4.2 lib/active_repository/finders.rb
active_repository-0.4.1 lib/active_repository/finders.rb
active_repository-0.4.0 lib/active_repository/finders.rb
active_repository-0.3.10 lib/active_repository/finders.rb
active_repository-0.3.9 lib/active_repository/finders.rb
active_repository-0.3.8 lib/active_repository/finders.rb
active_repository-0.3.7 lib/active_repository/finders.rb
active_repository-0.3.6 lib/active_repository/finders.rb
active_repository-0.3.5 lib/active_repository/finders.rb
active_repository-0.3.4 lib/active_repository/finders.rb
active_repository-0.3.3 lib/active_repository/finders.rb
active_repository-0.3.2 lib/active_repository/finders.rb
active_repository-0.3.1 lib/active_repository/finders.rb