Class: Hanami::Model::Adapters::RethinkdbAdapter Private

Inherits:
Abstract
  • Object
show all
Includes:
RethinkDB::Shortcuts
Defined in:
lib/hanami/model/adapters/rethinkdb_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 RethinkDB databases

See Also:

  • Implementation

Since:

  • 0.1.0

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mapper, uri, options = {}) ⇒ Hanami::Model::Adapters::RethinkdbAdapter

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.

Hanami::Model uses RethinkDB.

Parameters:

  • the database mapper

  • the database connection

See Also:

Since:

  • 0.1.0

API:

  • private



51
52
53
54
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 51

def initialize(mapper, uri, options={})
  super
  @connection = r.connect(_parse_uri)
end

Instance Attribute Details

#parsed_uriObject (readonly)

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.

Since:

  • 0.1.0

API:

  • private



35
36
37
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 35

def parsed_uri
  @parsed_uri
end

Instance Method Details

#all(collection) ⇒ Array

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 all the documents for the given collection

Parameters:

  • the target collection (it must be mapped).

Returns:

  • all the documents

Since:

  • 0.1.0

API:

  • private



129
130
131
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 129

def all(collection)
  query(collection).all
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 documents from the given collection.

Parameters:

  • the target collection (it must be mapped).

Since:

  • 0.1.0

API:

  • private



179
180
181
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 179

def clear(collection)
  command(query(collection)).clear
end

#command(query) ⇒ Hanami::Model::Adapters::Rethinkdb::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.

object to act on.

Parameters:

  • the query

Returns:

See Also:

Since:

  • 0.1.0

API:

  • private



194
195
196
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 194

def command(query)
  Rethinkdb::Command.new(query)
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 document in the database for the given entity. It assigns the id attribute, in case of success.

Parameters:

  • the target collection (it must be mapped).

  • the entity to create

Returns:

  • the entity

Since:

  • 0.1.0

API:

  • private



84
85
86
87
88
89
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 84

def create(collection, entity)
  ::Kernel.raise Adapters::RethinkdbIOError.new(collection, 'create') if entity.nil?
  command(
    query(collection)
  ).create(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 document in the database corresponding to the given entity.

Parameters:

  • the target collection (it must be mapped).

  • the entity to delete

Since:

  • 0.1.0

API:

  • private



114
115
116
117
118
119
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 114

def delete(collection, entity)
  ::Kernel.raise RethinkdbIOError.new(collection, 'delete') if entity.nil?
  command(
    _find(collection, entity.id)
  ).delete
end

#find(collection, id) ⇒ 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 a unique document from the given collection, with the given id.

Parameters:

  • the target collection (it must be mapped).

  • the identity of the object.

Returns:

  • the entity

Since:

  • 0.1.0

API:

  • private



143
144
145
146
147
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 143

def find(collection, id)
  _first(
    _find(collection, id)
  )
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. RethinkDB does not have sequential primary keys.

Parameters:

  • the target collection (it must be mapped)

Raises:

Since:

  • 0.1.0

API:

  • private



157
158
159
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 157

def first(_collection)
  fail 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. RethinkDB does not have sequential primary keys.

Parameters:

  • the target collection (it must be mapped)

Raises:

Since:

  • 0.1.0

API:

  • private



169
170
171
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 169

def last(_collection)
  fail NotImplementedError
end

#persist(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 or updates a document in the database for the given entity.

Parameters:

  • the target collection (it must be mapped).

  • the entity to persist

Returns:

  • the entity

Since:

  • 0.1.0

API:

  • private



65
66
67
68
69
70
71
72
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 65

def persist(collection, entity)
  ::Kernel.raise RethinkdbIOError.new(collection, 'IO') if entity.nil?
  if entity.id
    update(collection, entity)
  else
    create(collection, entity)
  end
end

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

  • the target collection (it must be mapped).

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

Returns:

See Also:

Since:

  • 0.1.0

API:

  • private



210
211
212
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 210

def query(collection, context = nil, &blk)
  Rethinkdb::Query.new(_collection(collection), context, &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 document in the database corresponding to the given entity.

Parameters:

  • the target collection (it must be mapped).

  • the entity to update

Returns:

  • the entity

Since:

  • 0.1.0

API:

  • private



100
101
102
103
104
105
# File 'lib/hanami/model/adapters/rethinkdb_adapter.rb', line 100

def update(collection, entity)
  ::Kernel.raise RethinkdbIOError.new(collection, 'update') if entity.nil?
  command(
    _find(collection, entity.id)
  ).update(entity)
end