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

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

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.



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.



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.



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

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