Class: Lotus::Model::Adapters::Memory::Collection Private

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

Acts like a SQL database table.

Since:

  • 0.1.0

Defined Under Namespace

Classes: PrimaryKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, identity) ⇒ 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:

  • name (Symbol)

    the name of the collection (eg. ‘:users`).

  • identity (Symbol)

    the primary key of the collection (eg. ‘:id`).

Since:

  • 0.1.0



64
65
66
67
# File 'lib/lotus/model/adapters/memory/collection.rb', line 64

def initialize(name, identity)
  @name, @identity = name, identity
  clear
end

Instance Attribute Details

#identityObject (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



48
49
50
# File 'lib/lotus/model/adapters/memory/collection.rb', line 48

def identity
  @identity
end

#nameObject (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



41
42
43
# File 'lib/lotus/model/adapters/memory/collection.rb', line 41

def name
  @name
end

#recordsObject (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



54
55
56
# File 'lib/lotus/model/adapters/memory/collection.rb', line 54

def records
  @records
end

Instance Method Details

#allArray<Hash>

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 raw records

Returns:

  • (Array<Hash>)

Since:

  • 0.1.0



116
117
118
# File 'lib/lotus/model/adapters/memory/collection.rb', line 116

def all
  records.values
end

#clearObject

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 and resets the identity counter.

Since:

  • 0.1.0



124
125
126
127
# File 'lib/lotus/model/adapters/memory/collection.rb', line 124

def clear
  @records     = {}
  @primary_key = PrimaryKey.new
end

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

Parameters:

  • entity (Object)

    the entity to persist

Returns:

  • the primary key of the created record

See Also:

Since:

  • 0.1.0



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

def create(entity)
  @primary_key.increment! do |id|
    entity[identity] = id
    records[id] = entity
  end
end

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

Parameters:

  • entity (Object)

    the entity to delete

See Also:

Since:

  • 0.1.0



106
107
108
# File 'lib/lotus/model/adapters/memory/collection.rb', line 106

def delete(entity)
  records.delete(entity.id)
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 record corresponding to the given entity.

Parameters:

  • entity (Object)

    the entity to persist

See Also:

Since:

  • 0.1.0



94
95
96
# File 'lib/lotus/model/adapters/memory/collection.rb', line 94

def update(entity)
  records[entity.fetch(identity)] = entity
end