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, #fetch, #find, #first, #last, #persist

Constructor Details

#initialize(mapper, uri = nil, options = {}) ⇒ 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)

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

    a hash of non mandatory adapter options

See Also:

Since:

  • 0.1.0



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

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



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

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



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

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



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

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



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

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

#disconnectObject

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.

See Also:

Since:

  • 0.5.0



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

def disconnect
  @collections = DisconnectedResource.new
  @mutex       = DisconnectedResource.new
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



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

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



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

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



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

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