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) ⇒ 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

Since:

  • 0.1.0



89
90
91
92
# File 'lib/lotus/model/adapters/abstract.rb', line 89

def initialize(mapper, uri = nil)
  @mapper = mapper
  @uri    = uri
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



148
149
150
# File 'lib/lotus/model/adapters/abstract.rb', line 148

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



192
193
194
# File 'lib/lotus/model/adapters/abstract.rb', line 192

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



201
202
203
# File 'lib/lotus/model/adapters/abstract.rb', line 201

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



242
243
244
# File 'lib/lotus/model/adapters/abstract.rb', line 242

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



115
116
117
# File 'lib/lotus/model/adapters/abstract.rb', line 115

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



137
138
139
# File 'lib/lotus/model/adapters/abstract.rb', line 137

def delete(collection, entity)
  raise NotImplementedError
end

#disconnectObject

Disconnects the connection by freeing low level resources

Raises:

  • (NotImplementedError)

Since:

  • 0.5.0



272
273
274
# File 'lib/lotus/model/adapters/abstract.rb', line 272

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



253
254
255
# File 'lib/lotus/model/adapters/abstract.rb', line 253

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



265
266
267
# File 'lib/lotus/model/adapters/abstract.rb', line 265

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



161
162
163
# File 'lib/lotus/model/adapters/abstract.rb', line 161

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



172
173
174
# File 'lib/lotus/model/adapters/abstract.rb', line 172

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



183
184
185
# File 'lib/lotus/model/adapters/abstract.rb', line 183

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



102
103
104
# File 'lib/lotus/model/adapters/abstract.rb', line 102

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



214
215
216
# File 'lib/lotus/model/adapters/abstract.rb', line 214

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



232
233
234
# File 'lib/lotus/model/adapters/abstract.rb', line 232

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



127
128
129
# File 'lib/lotus/model/adapters/abstract.rb', line 127

def update(collection, entity)
  raise NotImplementedError
end