Class: Hanami::Model::Adapters::Rethinkdb::Collection Private

Inherits:
SimpleDelegator
  • Object
show all
Includes:
RethinkDB::Shortcuts
Defined in:
lib/hanami/model/adapters/rethinkdb/collection.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.

Maps a RethinkDB database table and perfoms manipulations on it.

Since:

  • 0.1.0

Instance Method Summary collapse

Constructor Details

#initialize(connection, dataset, mapped_collection) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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 a collection

Parameters:

  • connection (RethinkDB::Connection)

    the connection to the database

  • dataset (RethinkDB::RQL)

    the dataset that maps a table or a subset of it

  • mapped_collection (Hanami::Model::Mapping::Collection)

    a mapped collection

Since:

  • 0.1.0



43
44
45
46
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 43

def initialize(connection, dataset, mapped_collection)
  super(dataset)
  @connection, @mapped_collection = connection, mapped_collection
end

Instance Method Details

#avg(*args) ⇒ Numeric

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 the average of the values for the given column.

Parameters:

  • args (Array)

    the array of arguments

Returns:

  • (Numeric)

See Also:

Since:

  • 0.1.0



202
203
204
205
206
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 202

def avg(*args)
  _run do
    super.default(nil)
  end
end

#countNumeric

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 count of the documents for the current conditions.

Parameters:

  • args (Array)

    the array of arguments

Returns:

  • (Numeric)

See Also:

Since:

  • 0.1.0



250
251
252
253
254
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 250

def count
  _run do
    super
  end
end

#deleteObject

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 the current scope.



94
95
96
97
98
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 94

def delete
  _run do
    super
  end
end

#filter(*args) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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.

Filters the current scope with a ‘filter` directive.

Parameters:

  • args (Array)

    the array of arguments

Returns:

See Also:

Since:

  • 0.1.0



111
112
113
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 111

def filter(*args)
  _collection(super, @mapped_collection)
end

#has_fields(*args) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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.

Filters the current scope with a ‘has_fields` directive.

Parameters:

  • args (Array)

    the array of arguments

Returns:

See Also:

Since:

  • 0.1.0



141
142
143
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 141

def has_fields(*args) # rubocop:disable Style/PredicateName
  _collection(super, @mapped_collection)
end

#insert(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 for the given entity and assigns an id.

Parameters:

  • entity (Object)

    the entity to persist

Returns:

  • the primary key of the created document

See Also:

Since:

  • 0.1.0



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 58

def insert(entity)
  serialized_entity = _serialize(entity)

  response = _run do
    super(serialized_entity)
  end

  serialized_entity[_identity] = response['generated_keys'].first

  _deserialize([serialized_entity]).first
end

#limit(*args) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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.

Filters the current scope with a ‘limit` directive.

Parameters:

  • args (Array)

    the array of arguments

Returns:

See Also:

Since:

  • 0.1.0



156
157
158
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 156

def limit(*args)
  _collection(super, @mapped_collection)
end

#max(field, *args) ⇒ Numeric

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 the maximum value for the given field.

Parameters:

  • args (Array)

    the array of arguments

Returns:

  • (Numeric)

See Also:

Since:

  • 0.1.0



218
219
220
221
222
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 218

def max(field, *args)
  _run do
    super[field].default(nil)
  end
end

#min(field, *args) ⇒ Numeric

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 the minimum value for the given field.

Parameters:

  • args (Array)

    the array of arguments

Returns:

  • (Numeric)

See Also:

Since:

  • 0.1.0



234
235
236
237
238
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 234

def min(field, *args)
  _run do
    super[field].default(nil)
  end
end

#order_by(*args) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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.

Filters the current scope with an ‘order_by` directive.

Parameters:

  • args (Array)

    the array of arguments

Returns:

See Also:

Since:

  • 0.1.0



172
173
174
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 172

def order_by(*args)
  _collection(super, @mapped_collection)
end

#pluck(*args) ⇒ Hanami::Model::Adapters::Rethinkdb::Collection

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.

Filters the current scope with a ‘pluck` directive.

Parameters:

  • args (Array)

    the array of arguments

Returns:

See Also:

Since:

  • 0.1.0



126
127
128
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 126

def pluck(*args)
  _collection(super, @mapped_collection)
end

#sum(*args) ⇒ Numeric

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 the sum of the values for the given field.

Parameters:

  • args (Array)

    the array of arguments

Returns:

  • (Numeric)

See Also:

Since:

  • 0.1.0



186
187
188
189
190
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 186

def sum(*args)
  _run do
    super
  end
end

#to_aArray Also known as: execute

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.

Resolves self by fetching the documents from the database and translating them into entities.

Returns:

  • (Array)

    the result of the query

Since:

  • 0.1.0



263
264
265
266
267
268
269
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 263

def to_a
  _deserialize(
    _run do
      self
    end
  )
end

#update(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 the document corresponding to the given entity.

Parameters:

  • entity (Object)

    the entity to persist

See Also:

Since:

  • 0.1.0



78
79
80
81
82
83
84
85
86
# File 'lib/hanami/model/adapters/rethinkdb/collection.rb', line 78

def update(entity)
  serialized_entity = _serialize(entity)

  response = _run do
    super(serialized_entity)
  end

  _deserialize([serialized_entity]).first
end