Class: Lotus::Model::Adapters::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lotus/model/adapters/abstract.rb

Overview

Abstract adapter.

An adapter is a concrete implementation that allows a repository to communicate with a single database.

Lotus::Model is shipped with Memory and SQL adapters. Third part adapters MUST implement the interface defined here. For convenience they may inherit from this class.

These are low level details, and shouldn’t be used directly. Please use a repository for entities persistence.

Since:

  • 0.1.0

Direct Known Subclasses

MemoryAdapter, SqlAdapter

Instance Method Summary collapse

Constructor Details

#initialize(mapper, uri = nil, options = {}) ⇒ Abstract

Initialize the adapter

Parameters:

  • mapper (Lotus::Model::Mapper)

    the object that defines the database to entities mapping

  • uri (String) (defaults to: nil)

    the optional connection string to the database

  • options (Hash) (defaults to: {})

    a list of non-mandatory adapter options

Since:

  • 0.1.0



91
92
93
94
95
# File 'lib/lotus/model/adapters/abstract.rb', line 91

def initialize(mapper, uri = nil, options = {})
  @mapper = mapper
  @uri    = uri
  @options = options
end

Instance Method Details

#all(collection) ⇒ Array

Returns all the records for the given collection

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Array)

    all the records

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



151
152
153
# File 'lib/lotus/model/adapters/abstract.rb', line 151

def all(collection)
  raise NotImplementedError
end

#clear(collection) ⇒ Object

Empties the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



195
196
197
# File 'lib/lotus/model/adapters/abstract.rb', line 195

def clear(collection)
  raise NotImplementedError
end

#command(query) ⇒ Object

Executes a command for the given query.

Parameters:

  • query (Object)

    the query object to act on.

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



204
205
206
# File 'lib/lotus/model/adapters/abstract.rb', line 204

def command(query)
  raise NotImplementedError
end

#connection_stringString

Returns a string which can be executed to start a console suitable for the configured database.

Returns:

  • (String)

    to be executed to start a database console

Raises:

Since:

  • 0.3.0



245
246
247
# File 'lib/lotus/model/adapters/abstract.rb', line 245

def connection_string
  raise NotSupportedError
end

#create(collection, entity) ⇒ Object

Creates a record in the database for the given entity. It should assign an id (identity) to the entity in case of success.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (Object)

    the entity to create

Returns:

  • (Object)

    the entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



118
119
120
# File 'lib/lotus/model/adapters/abstract.rb', line 118

def create(collection, entity)
  raise NotImplementedError
end

#delete(collection, entity) ⇒ Object

Deletes a record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (Object)

    the entity to delete

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



140
141
142
# File 'lib/lotus/model/adapters/abstract.rb', line 140

def delete(collection, entity)
  raise NotImplementedError
end

#disconnectObject

Disconnects the connection by freeing low level resources

Raises:

  • (NotImplementedError)

Since:

  • 0.5.0



275
276
277
# File 'lib/lotus/model/adapters/abstract.rb', line 275

def disconnect
  raise NotImplementedError
end

#execute(raw) ⇒ NilClass

Executes a raw command

Parameters:

  • raw (String)

    the raw statement to execute on the connection

Returns:

  • (NilClass)

Raises:

  • (NotImplementedError)

Since:

  • 0.3.1



256
257
258
# File 'lib/lotus/model/adapters/abstract.rb', line 256

def execute(raw)
  raise NotImplementedError
end

#fetch(raw, &blk) ⇒ Enumerable<Hash>, Array<Hash>

Fetches raw records from

Parameters:

  • raw (String)

    the raw query

  • blk (Proc)

    an optional block that is yielded for each record

Returns:

  • (Enumerable<Hash>, Array<Hash>)

Raises:

  • (NotImplementedError)

Since:

  • 0.5.0



268
269
270
# File 'lib/lotus/model/adapters/abstract.rb', line 268

def fetch(raw, &blk)
  raise NotImplementedError
end

#find(collection, id) ⇒ Object

Returns a unique record from the given collection, with the given identity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • id (Object)

    the identity of the object.

Returns:

  • (Object)

    the entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



164
165
166
# File 'lib/lotus/model/adapters/abstract.rb', line 164

def find(collection, id)
  raise NotImplementedError
end

#first(collection) ⇒ Object

Returns the first record in the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the first entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



175
176
177
# File 'lib/lotus/model/adapters/abstract.rb', line 175

def first(collection)
  raise NotImplementedError
end

#last(collection) ⇒ Object

Returns the last record in the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the last entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



186
187
188
# File 'lib/lotus/model/adapters/abstract.rb', line 186

def last(collection)
  raise NotImplementedError
end

#persist(collection, entity) ⇒ Object

Creates or updates a record in the database for the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (Object)

    the entity to persist

Returns:

  • (Object)

    the entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



105
106
107
# File 'lib/lotus/model/adapters/abstract.rb', line 105

def persist(collection, entity)
  raise NotImplementedError
end

#query(collection, &blk) ⇒ Object

Returns a query

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • blk (Proc)

    a block of code to be executed in the context of the query.

Returns:

  • (Object)

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



217
218
219
# File 'lib/lotus/model/adapters/abstract.rb', line 217

def query(collection, &blk)
  raise NotImplementedError
end

#transaction(options = {}) ⇒ Object

Wraps the given block in a transaction.

For performance reasons the block isn’t in the signature of the method, but it’s yielded at the lower level.

Please note that it’s only supported by some databases. For this reason, the options may vary from adapter to adapter.

Parameters:

  • options (Hash) (defaults to: {})

    options for transaction

Raises:

  • (NotImplementedError)

See Also:

Since:

  • 0.2.3



235
236
237
# File 'lib/lotus/model/adapters/abstract.rb', line 235

def transaction(options = {})
  raise NotImplementedError
end

#update(collection, entity) ⇒ Object

Updates a record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (Object)

    the entity to update

Returns:

  • (Object)

    the entity

Raises:

  • (NotImplementedError)

Since:

  • 0.1.0



130
131
132
# File 'lib/lotus/model/adapters/abstract.rb', line 130

def update(collection, entity)
  raise NotImplementedError
end