Module: S3DB::Record
- Defined in:
- lib/s3db/record.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Instance Method Summary collapse
- #_id ⇒ Object
- #_id=(id) ⇒ Object
-
#initialize(data) ⇒ Object
Instantiate a new record.
- #new_record? ⇒ Boolean
-
#save ⇒ Object
Save an instantiated record.
-
#save! ⇒ Object
Save an instantiated record, raising an error on failure.
-
#update(data) ⇒ Object
Update the data for a record and save.
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/s3db/record.rb', line 3 def data @data end |
Instance Method Details
#_id ⇒ Object
155 156 157 |
# File 'lib/s3db/record.rb', line 155 def _id @data['_id'] end |
#_id=(id) ⇒ Object
151 152 153 |
# File 'lib/s3db/record.rb', line 151 def _id=(id) @data['_id'] = id end |
#initialize(data) ⇒ Object
Instantiate a new record.
data - Hash of data. Required.
returns a new instance of the record.
96 97 98 99 100 101 102 103 |
# File 'lib/s3db/record.rb', line 96 def initialize(data) hash = {} data.each_pair do |k,v| hash[k.to_s] = v end @data = hash end |
#new_record? ⇒ Boolean
105 106 107 |
# File 'lib/s3db/record.rb', line 105 def new_record? _id.nil? end |
#save ⇒ Object
Save an instantiated record.
returns the record on success or failure.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/s3db/record.rb', line 112 def save _set_id return false if _id.nil? self.class._database.backend.write_record( self.class._database.name, self.class._collection.name, _filename, @data.to_json ) self end |
#save! ⇒ Object
Save an instantiated record, raising an error on failure.
returns the record on success; raises an error on failure.
130 131 132 |
# File 'lib/s3db/record.rb', line 130 def save! save || raise(ArgumentError, 'failed to save!') end |
#update(data) ⇒ Object
Update the data for a record and save.
data - Hash of data for the record. Required.
returns #save.
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/s3db/record.rb', line 139 def update(data) return false if _id.nil? # Copy the existing id to the new data, if it exists. data.merge('_id' => _id) # Update the dataset @data = data save end |