Class: Lotus::Model::Adapters::MemoryAdapter Private

Inherits:
Abstract
  • Object
show all
Includes:
Implementation
Defined in:
lib/lotus/model/adapters/memory_adapter.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

In memory adapter that behaves like a SQL database. Not all the features of the SQL adapter are supported.

This adapter SHOULD be used only for development or testing purposes, because its computations are inefficient and the data is volatile.

See Also:

Since:

  • 0.1.0

Direct Known Subclasses

FileSystemAdapter

Instance Method Summary collapse

Methods included from Implementation

#all, #find, #first, #last, #persist

Methods inherited from Abstract

#all, #connection_string, #execute, #find, #first, #last, #persist

Constructor Details

#initialize(mapper, uri = nil) ⇒ Lotus::Model::Adapters::MemoryAdapter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the adapter.

Parameters:

  • mapper (Object)

    the database mapper

  • uri (String) (defaults to: nil)

    the connection uri (ignored)

See Also:

Since:

  • 0.1.0



34
35
36
37
38
39
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 34

def initialize(mapper, uri = nil)
  super

  @mutex       = Mutex.new
  @collections = {}
end

Instance Method Details

#clear(collection) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deletes all the records from the given collection and resets the identity counter.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Since:

  • 0.1.0



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

def clear(collection)
  synchronize do
    command(collection).clear
  end
end

#command(collection) ⇒ Lotus::Model::Adapters::Memory::Command

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fabricates a command for the given query.

Parameters:

  • collection (Symbol)

    the collection name (it must be mapped)

Returns:

See Also:

Since:

  • 0.1.0



108
109
110
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 108

def command(collection)
  Memory::Command.new(_collection(collection), _mapped_collection(collection))
end

#create(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a record in the database for the given entity. It assigns the ‘id` attribute, in case of success.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id=)

    the entity to create

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



51
52
53
54
55
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 51

def create(collection, entity)
  synchronize do
    command(collection).create(entity)
  end
end

#delete(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to delete

Since:

  • 0.1.0



79
80
81
82
83
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 79

def delete(collection, entity)
  synchronize do
    command(collection).delete(entity)
  end
end

#query(collection, context = nil, &blk) ⇒ Lotus::Model::Adapters::Memory::Query

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Fabricates 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:

See Also:

Since:

  • 0.1.0



124
125
126
127
128
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 124

def query(collection, context = nil, &blk)
  synchronize do
    Memory::Query.new(_collection(collection), _mapped_collection(collection), &blk)
  end
end

#transaction(options = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

WARNING: this is a no-op. For “real” transactions please use ‘SqlAdapter` or another adapter that supports them

Parameters:

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

    options for transaction

See Also:

Since:

  • 0.2.3



139
140
141
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 139

def transaction(options = {})
  yield
end

#update(collection, entity) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to update

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



66
67
68
69
70
# File 'lib/lotus/model/adapters/memory_adapter.rb', line 66

def update(collection, entity)
  synchronize do
    command(collection).update(entity)
  end
end