Class: Lotus::Model::Adapters::FileSystemAdapter Private

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

In memory adapter with file system persistence. It behaves like the SQL adapter, but it doesn’t support all the SQL features offered by that kind of databases.

This adapter SHOULD be used only for development or testing purposes. Each read/write operation is wrapped by a ‘Mutex` and persisted to the disk.

For those reasons it’s really unefficient, but great for quick prototyping as it’s schema-less.

It works exactly like the ‘MemoryAdapter`, with the only difference that it persist data to the disk.

The persistence policy uses Ruby ‘Marshal` `dump` and `load` operations. Please be aware of the limitations this model.

Constant Summary collapse

WRITING_MODE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default writing mode

Binary, write only, create file if missing or erase if don’t.

File::WRONLY|File::BINARY|File::CREAT
CHMOD =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default chmod

See Also:

Since:

  • 0.2.0

0644
FILE_SCHEME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

File scheme

See Also:

Since:

  • 0.2.0

'file:///'.freeze

Instance Method Summary collapse

Methods inherited from MemoryAdapter

#command, #transaction

Methods included from Implementation

#persist

Methods inherited from Abstract

#command, #connection_string, #execute, #fetch, #persist, #transaction

Constructor Details

#initialize(mapper, uri) ⇒ Lotus::Model::Adapters::FileSystemAdapter

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.

Parameters:

  • mapper (Object)

    the database mapper

  • uri (String)

    the connection uri

See Also:

Since:

  • 0.2.0



69
70
71
72
73
74
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 69

def initialize(mapper, uri)
  super
  prepare(uri)

  @_mutex = Mutex.new
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 records for the given collection

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Array)

    all the records

Since:

  • 0.2.0



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

def all(collection)
  _synchronize do
    read(collection)
    super
  end
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 records from the given collection and resets the identity counter.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Since:

  • 0.2.0



190
191
192
193
194
195
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 190

def clear(collection)
  _synchronize do
    super
    write(collection)
  end
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 record in the database for the given entity. It assigns the ‘id` attribute, in case of success.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id=)

    the entity to create

Returns:

  • (Object)

    the entity

Since:

  • 0.2.0



148
149
150
151
152
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 148

def create(collection, entity)
  _synchronize do
    super.tap { write(collection) }
  end
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 record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to delete

Since:

  • 0.2.0



176
177
178
179
180
181
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 176

def delete(collection, entity)
  _synchronize do
    super
    write(collection)
  end
end

#disconnectObject

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.

See Also:

Since:

  • 0.5.0



232
233
234
235
236
237
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 232

def disconnect
  super

  @_mutex = DisconnectedResource.new
  @root   = DisconnectedResource.new
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 record from the given collection, with the given id.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • id (Object)

    the identity of the object.

Returns:

  • (Object)

    the entity

Since:

  • 0.2.0



101
102
103
104
105
106
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 101

def find(collection, id)
  _synchronize do
    read(collection)
    super
  end
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.

Returns the first record in the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the first entity

Since:

  • 0.2.0



116
117
118
119
120
121
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 116

def first(collection)
  _synchronize do
    read(collection)
    super
  end
end

#infoHash

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.

Database informations

Returns:

  • (Hash)

    per collection informations

Since:

  • 0.2.0



222
223
224
225
226
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 222

def info
  @collections.each_with_object({}) do |(collection,_), result|
    result[collection] = query(collection).count
  end
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.

Returns the last record in the given collection.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the last entity

Since:

  • 0.2.0



131
132
133
134
135
136
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 131

def last(collection)
  _synchronize do
    read(collection)
    super
  end
end

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

  • collection (Symbol)

    the target collection (it must be mapped).

  • blk (Proc)

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

Returns:

See Also:

Since:

  • 0.2.0



209
210
211
212
213
214
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 209

def query(collection, context = nil, &blk)
  # _synchronize do
    read(collection)
    super
  # end
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 record in the database corresponding to the given entity.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id)

    the entity to update

Returns:

  • (Object)

    the entity

Since:

  • 0.2.0



163
164
165
166
167
# File 'lib/lotus/model/adapters/file_system_adapter.rb', line 163

def update(collection, entity)
  _synchronize do
    super.tap { write(collection) }
  end
end