Module: S3DB::Record::ClassMethods
- Defined in:
- lib/s3db/record.rb
Instance Attribute Summary collapse
-
#_collection ⇒ Object
Returns the value of attribute _collection.
-
#_database ⇒ Object
Returns the value of attribute _database.
-
#_id_field ⇒ Object
Returns the value of attribute _id_field.
-
#_id_generator ⇒ Object
Returns the value of attribute _id_generator.
-
#_schema ⇒ Object
Returns the value of attribute _schema.
Instance Method Summary collapse
-
#all ⇒ Object
Locate all records in the collection.
-
#collection_name(name) ⇒ Object
Set the collection to a new collection with the passed name.
-
#create(data) ⇒ Object
Create a new record, and write it to disk.
-
#database(database) ⇒ Object
Set the database to use for the record.
-
#find(filename) ⇒ Object
Locate a single record from the collection by filename.
-
#id_field(key) ⇒ Object
Set the field to use for the id.
-
#id_generator(proc) ⇒ Object
Set the id generator for new records.
-
#string(key) ⇒ Object
Set a field on the record to validate as a string.
Instance Attribute Details
#_collection ⇒ Object
Returns the value of attribute _collection.
6 7 8 |
# File 'lib/s3db/record.rb', line 6 def _collection @_collection end |
#_database ⇒ Object
Returns the value of attribute _database.
6 7 8 |
# File 'lib/s3db/record.rb', line 6 def _database @_database end |
#_id_field ⇒ Object
Returns the value of attribute _id_field.
6 7 8 |
# File 'lib/s3db/record.rb', line 6 def _id_field @_id_field end |
#_id_generator ⇒ Object
Returns the value of attribute _id_generator.
6 7 8 |
# File 'lib/s3db/record.rb', line 6 def _id_generator @_id_generator end |
#_schema ⇒ Object
Returns the value of attribute _schema.
6 7 8 |
# File 'lib/s3db/record.rb', line 6 def _schema @_schema end |
Instance Method Details
#all ⇒ Object
Locate all records in the collection.
returns the output from S3DB::Collection#list_records.
59 60 61 62 63 |
# File 'lib/s3db/record.rb', line 59 def all instance_variable_get(:@_collection).list_records.map do |rec| new(JSON.parse(rec)) end end |
#collection_name(name) ⇒ Object
Set the collection to a new collection with the passed name.
name - String name of the collection. Required.
returns the newly created S3DB::Collection.
22 23 24 |
# File 'lib/s3db/record.rb', line 22 def collection_name(name) self._collection = S3DB::Collection.create(_database, name) end |
#create(data) ⇒ Object
Create a new record, and write it to disk.
data - Hash data for the record.
returns an instance of the record.
84 85 86 87 88 |
# File 'lib/s3db/record.rb', line 84 def create(data) record = new(data) record.__send__(:_set_id) record.save end |
#database(database) ⇒ Object
Set the database to use for the record.
database - S3DB::Database instance. Required.
Returns the database instance.
13 14 15 |
# File 'lib/s3db/record.rb', line 13 def database(database) self._database = database end |
#find(filename) ⇒ Object
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.
70 71 72 73 74 75 76 77 |
# 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 |
#id_field(key) ⇒ Object
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.
41 42 43 |
# File 'lib/s3db/record.rb', line 41 def id_field(key) self._id_field = key.to_s end |
#id_generator(proc) ⇒ Object
Set the id generator for new records.
proc - Proc to call, returning the id field. Required.
returns the Proc instance.
31 32 33 |
# File 'lib/s3db/record.rb', line 31 def id_generator(proc) self._id_generator = proc end |
#string(key) ⇒ Object
Set a field on the record to validate as a string.
key - String name of key. Required.
returns the schema.
50 51 52 53 54 |
# 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 |