Class: Fauna::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/fauna/context.rb

Overview

The client context wrapper.

Used for accessing the client without directly passing around the client instance. Context is scoped to the current thread.

Class Method Summary collapse

Class Method Details

.block(client) ⇒ Object

Returns a context block with the given client.

client

Client to use for the context block.



16
17
18
19
20
21
# File 'lib/fauna/context.rb', line 16

def self.block(client)
  push(client)
  yield
ensure
  pop
end

.clientObject

Returns the current context’s client, or if there is none, raises NoContextError.



134
135
136
# File 'lib/fauna/context.rb', line 134

def self.client
  stack.last || fail(NoContextError, 'You must be within a Fauna::Context.block to perform operations.')
end

.delete(path) ⇒ Object

Performs a DELETE request for a REST endpoint within the current client context.

path

Path to DELETE.

data

Data to post as the body.

Reference: FaunaDB REST API

:category: Client Methods



104
105
106
# File 'lib/fauna/context.rb', line 104

def self.delete(path)
  client.delete(path)
end

.get(path, query = {}) ⇒ Object

Performs a GET request for a REST endpoint within the current client context.

path

Path to GET.

query

Query parameters to append to the path.

Reference: FaunaDB REST API

:category: Client Methods



52
53
54
# File 'lib/fauna/context.rb', line 52

def self.get(path, query = {})
  client.get(path, query)
end

.paginate(set, params = {}, &fauna_map) ⇒ Object

Creates a Fauna::Page for paging/iterating over a set with the current client context.

set

A set query to paginate over.

params

A list of parameters to pass to paginate.

fauna_map

Optional block to wrap the generated paginate query with. The block will be run in a query context. The paginate query will be passed into the block as an argument.



128
129
130
# File 'lib/fauna/context.rb', line 128

def self.paginate(set, params = {}, &fauna_map)
  client.paginate(set, params, &fauna_map)
end

.patch(path, data = {}) ⇒ Object

Performs a PATCH request for a REST endpoint within the current client context.

path

Path to PATCH.

data

Data to post as the body.

Reference: FaunaDB REST API

:category: Client Methods



91
92
93
# File 'lib/fauna/context.rb', line 91

def self.patch(path, data = {})
  client.patch(path, data)
end

.popObject

Removes the last client context from the stack and returns it.



33
34
35
# File 'lib/fauna/context.rb', line 33

def self.pop
  stack.pop
end

.post(path, data = {}) ⇒ Object

Performs a POST request for a REST endpoint within the current client context.

path

Path to POST.

data

Data to post as the body.

Reference: FaunaDB REST API

:category: Client Methods



65
66
67
# File 'lib/fauna/context.rb', line 65

def self.post(path, data = {})
  client.post(path, data)
end

.push(client) ⇒ Object

Adds a client to the current context.

client

Client to add to the current context.



27
28
29
# File 'lib/fauna/context.rb', line 27

def self.push(client)
  stack.push(client)
end

.put(path, data = {}) ⇒ Object

Performs a PUT request for a REST endpoint within the current client context.

path

Path to PUT.

data

Data to post as the body.

Reference: FaunaDB REST API

:category: Client Methods



78
79
80
# File 'lib/fauna/context.rb', line 78

def self.put(path, data = {})
  client.put(path, data)
end

.query(expression = nil, &expr_block) ⇒ Object

Issues a query to FaunaDB with the current client context.

Queries are built via the Query helpers. See FaunaDB Query API for information on constructing queries.

expression

A query expression

:category: Client Methods



117
118
119
# File 'lib/fauna/context.rb', line 117

def self.query(expression = nil, &expr_block)
  client.query(expression, &expr_block)
end

.resetObject

Resets the current client context, removing all the clients from the stack.



39
40
41
# File 'lib/fauna/context.rb', line 39

def self.reset
  stack.clear
end