class S3DB::Collection
Attributes
database[R]
name[R]
Public Class Methods
create(database, name)
click to toggle source
Create a new collection.
database - Database
attached to collection. Required. name - String name of the collection. Required.
returns a new Collection
.
# File lib/s3db/collection.rb, line 12 def create(database, name) collection = new(database, name) collection.save collection end
new(database, name) { |self| ... }
click to toggle source
Instantiate a new collection, without writing it to disk.
database - Database
attached to collection. Required. name - String name of the collection. Required.
returns a new Collection
, validated but unwritten.
# File lib/s3db/collection.rb, line 26 def initialize(database, name) # Store the database and collection name @database = database @name = Utils.sanitize(name) # Sanity check the database and collection name validate! # Yield self for configs, if people want to. yield self if block_given? end
Public Instance Methods
list_records()
click to toggle source
# File lib/s3db/collection.rb, line 54 def list_records @database.backend.list_records(@database.name, @name).map do |file| @database.backend.read_record(@database.name, @name, file) end end
save()
click to toggle source
Write the collection skeleton to disk.
Returns nil.
# File lib/s3db/collection.rb, line 63 def save @database.backend.write_collection(@database.name, @name) @database.backend.write_schema(@database.name, @name, @schema.to_json) nil end
validate!()
click to toggle source
Validate a collection to ensure that it’s sane.
returns nil on success; raises an error on failure.
# File lib/s3db/collection.rb, line 42 def validate! unless @database.is_a?(S3DB::Database) raise ArgumentError, 'database must be an S3DB::Database!' end unless @name.is_a?(String) raise ArgumentError, 'name must be a String!' end nil end