Class: Lotus::Model::Adapters::DynamodbAdapter Private

Inherits:
Abstract
  • Object
show all
Includes:
Implementation
Defined in:
lib/lotus/model/adapters/dynamodb_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.

Adapter for Amazon DynamoDB.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(mapper) ⇒ Lotus::Model::Adapters::DynamodbAdapter

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.

It takes advantage of AWS::DynamoDB::Client to perform all operations.

Parameters:

  • mapper (Object)

    the database mapper

See Also:

Since:

  • 0.1.0



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

def initialize(mapper)
  super

  @client = AWS::DynamoDB::Client.new(
    api_version: Lotus::Dynamodb::API_VERSION
  )
  @collections = {}
end

Instance Method Details

#_collection(name) ⇒ Lotus::Model::Adapters::Dynamodb::Collection (private)

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.

Returns a collection from the given name.

Parameters:

  • name (Symbol)

    a name of the collection (it must be mapped)

Returns:

See Also:

Since:

  • 0.1.0



180
181
182
183
184
185
186
187
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 180

def _collection(name)
  @collections[name] ||= Dynamodb::Collection.new(
    @client,
    Lotus::Model::Adapters::Dynamodb::Coercer.new(_mapped_collection(name)),
    name,
    _identity(name),
  )
end

#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.

This works terribly slow at the moment, and this is only useful for testing small collections. Consider re-creating table from scratch.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped)

Since:

  • 0.1.0



90
91
92
93
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 90

def clear(collection)
  blk = Proc.new {}
  query(collection, blk).each { |entity| delete(collection, entity) }
end

#command(collection) ⇒ Lotus::Model::Adapters::Dynamodb::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 target collection (it must be mapped)

Returns:

See Also:

Since:

  • 0.1.0



147
148
149
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 147

def command(collection)
  Dynamodb::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
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 52

def create(collection, entity)
  entity.id = command(collection).create(entity)
  entity
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



77
78
79
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 77

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

#find(collection, *key) ⇒ 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.

Returns an unique record from the given collection, with the given id.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped)

  • key (Array)

    the identity of the object

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



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

def find(collection, *key)
  command(collection).get(key)
end

#first(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.

This method is not implemented. DynamoDB does not allow table-wide sorting.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped)

Raises:

  • (NotImplementedError)

See Also:

Since:

  • 0.1.0



119
120
121
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 119

def first(collection)
  raise NotImplementedError
end

#last(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.

This method is not implemented. DynamoDB does not allow table-wide sorting.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped)

Raises:

  • (NotImplementedError)

See Also:

Since:

  • 0.1.0



133
134
135
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 133

def last(collection)
  raise NotImplementedError
end

#query(collection, context = nil, &blk) ⇒ Lotus::Model::Adapters::Dynamodb::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)

  • context (Object) (defaults to: nil)
  • blk (Proc)

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

Returns:

See Also:

Since:

  • 0.1.0



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

def query(collection, context = nil, &blk)
  Dynamodb::Query.new(_collection(collection), _mapped_collection(collection), &blk)
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
# File 'lib/lotus/model/adapters/dynamodb_adapter.rb', line 66

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