Class: Lotus::Model::Adapters::FileSystemAdapter Private
- Inherits:
-
MemoryAdapter
- Object
- Abstract
- MemoryAdapter
- Lotus::Model::Adapters::FileSystemAdapter
- 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
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
'file:///'.freeze
Instance Method Summary collapse
-
#all(collection) ⇒ Array
private
Returns all the records for the given collection.
-
#clear(collection) ⇒ Object
private
Deletes all the records from the given collection and resets the identity counter.
-
#create(collection, entity) ⇒ Object
private
Creates a record in the database for the given entity.
-
#delete(collection, entity) ⇒ Object
private
Deletes a record in the database corresponding to the given entity.
- #disconnect ⇒ Object private
-
#find(collection, id) ⇒ Object
private
Returns a unique record from the given collection, with the given id.
-
#first(collection) ⇒ Object
private
Returns the first record in the given collection.
-
#info ⇒ Hash
private
Database informations.
-
#initialize(mapper, uri) ⇒ Lotus::Model::Adapters::FileSystemAdapter
constructor
private
Initialize the adapter.
-
#last(collection) ⇒ Object
private
Returns the last record in the given collection.
-
#query(collection, context = nil, &blk) ⇒ Lotus::Model::Adapters::Memory::Query
private
Fabricates a query.
-
#update(collection, entity) ⇒ Object
private
Updates a record in the database corresponding to the given entity.
Methods inherited from MemoryAdapter
Methods included from Implementation
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.
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
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.
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.
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.
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 |
#disconnect ⇒ 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.
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.
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.
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 |
#info ⇒ 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.
Database informations
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.
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
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.
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 |