Class: S3DB::Database
- Inherits:
-
Object
- Object
- S3DB::Database
- Defined in:
- lib/s3db/database.rb
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.create(backend, db_name) ⇒ Object
Create a new database.
-
.drop(backend, db_name) ⇒ Object
Drop a database.
Instance Method Summary collapse
-
#create_collection(collection, schema = {}) ⇒ Object
Create a collection under this database.
-
#drop_collection(collection) ⇒ Object
Drop a collection from this database.
-
#initialize(backend, db_name) {|_self| ... } ⇒ Database
constructor
Create a new DB instance.
-
#path ⇒ Object
Locate the storage location for the database.
-
#save ⇒ Object
Save a DB instance to disk.
-
#show_collections ⇒ Object
List all available collections in the database.
Constructor Details
#initialize(backend, db_name) {|_self| ... } ⇒ Database
Create a new DB instance.
backend - S3DB::Backend subclass. Required. db_name - String name of database. Will be used in the storage path,
so make sure it's something sane. Required.
returns a new Database instance.
40 41 42 43 44 45 |
# File 'lib/s3db/database.rb', line 40 def initialize(backend, db_name) @backend = backend @name = db_name yield self if block_given? end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
3 4 5 |
# File 'lib/s3db/database.rb', line 3 def backend @backend end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/s3db/database.rb', line 3 def name @name end |
Class Method Details
.create(backend, db_name) ⇒ Object
Create a new database.
backend - S3DB::Backend subclass. Required. db_name - String name of the database to create. Required.
returns a new S3DB::Database on success, raises an error on failure.
12 13 14 15 16 17 18 19 20 |
# File 'lib/s3db/database.rb', line 12 def create(backend, db_name) begin backend.write_db(db_name) rescue Errno::EEXIST raise ArgumentError, 'database exists!' end new(backend, db_name) end |
.drop(backend, db_name) ⇒ Object
Drop a database.
backend - S3DB::Backend subclass. Required. db_name - String name of database to drop. Required.
returns the String database name on success, raises an error on failure.
28 29 30 |
# File 'lib/s3db/database.rb', line 28 def drop(backend, db_name) backend.delete_db(db_name) end |
Instance Method Details
#create_collection(collection, schema = {}) ⇒ Object
Create a collection under this database.
collection - String name of collection to create. Will also be used as
its filesystem path, so use something sane. Required.
schema - Hash schema for this collection. Default: {}.
returns true on success.
70 71 72 |
# File 'lib/s3db/database.rb', line 70 def create_collection(collection, schema = {}) Collection.create(self, collection) end |
#drop_collection(collection) ⇒ Object
Drop a collection from this database.
collection - String name of collection to drop.
returns the name of the collection on success.
79 80 81 |
# File 'lib/s3db/database.rb', line 79 def drop_collection(collection) @backend.delete_collection(@name, collection) end |
#path ⇒ Object
Locate the storage location for the database.
returns a String path of the database path. Will vary by backend adapter.
86 87 88 |
# File 'lib/s3db/database.rb', line 86 def path @backend.db_path(@name) end |
#save ⇒ Object
Save a DB instance to disk.
Returns a Database instance.
50 51 52 53 54 |
# File 'lib/s3db/database.rb', line 50 def save @backend.write_db(@name) self end |
#show_collections ⇒ Object
List all available collections in the database.
returns sorted Array of Strings.
59 60 61 |
# File 'lib/s3db/database.rb', line 59 def show_collections @backend.list_collections(@name).sort end |