module S3DB::Record::ClassMethods
Attributes
Public Instance Methods
Locate all records in the collection.
returns the output from S3DB::Collection#list_records
.
# File lib/s3db/record.rb, line 59 def all instance_variable_get(:@_collection).list_records.map do |rec| new(JSON.parse(rec)) end end
Set the collection to a new collection with the passed name.
name - String name of the collection. Required.
returns the newly created S3DB::Collection
.
# File lib/s3db/record.rb, line 22 def collection_name(name) self._collection = S3DB::Collection.create(_database, name) end
Create a new record, and write it to disk.
data - Hash data for the record.
returns an instance of the record.
# File lib/s3db/record.rb, line 84 def create(data) record = new(data) record.__send__(:_set_id) record.save end
Set the database to use for the record.
database - S3DB::Database
instance. Required.
Returns the database instance.
# File lib/s3db/record.rb, line 13 def database(database) self._database = database end
Locate a single record from the collection by filename.
filename - String filename to return. Required.
returns the record on success; raises an error on failure.
# File lib/s3db/record.rb, line 70 def find(filename) record = new(_id: filename) res = _database.backend.read_record(_database.name, _collection.name, record.__send__(:_filename)) raise ArgumentError, 'missing record!' unless res new(JSON.parse(res)) end
Set the field to use for the id. It will be passed to the id_generator
proc.
id_field
- String name of field. Required.
returns the id_field.
# File lib/s3db/record.rb, line 41 def id_field(key) self._id_field = key.to_s end
Set the id generator for new records.
proc - Proc to call, returning the id field. Required.
returns the Proc instance.
# File lib/s3db/record.rb, line 31 def id_generator(proc) self._id_generator = proc end
Set a field on the record to validate as a string.
key - String name of key. Required.
returns the schema.
# File lib/s3db/record.rb, line 50 def string(key) schema = instance_variable_get(:@_schema) || {} schema[key.to_s] = 'String' instance_variable_set(:@_schema, schema) end