Module: AWS::Record::AbstractBase::InstanceMethods
- Defined in:
- lib/aws/record/abstract_base.rb
Instance Method Summary collapse
-
#attributes ⇒ Hash
A hash with attribute names as hash keys (strings) and attribute values (of mixed types) as hash values.
-
#attributes=(attributes) ⇒ Hash
Acts like #update but does not call #save.
-
#delete ⇒ true
Deletes the record.
-
#deleted? ⇒ Boolean
Returns true if this instance object has been deleted.
- #errors ⇒ Object
-
#id ⇒ String
The id for each record is auto-generated.
-
#initialize(attributes = {}) ⇒ Model, HashModel
Constructs a new record.
-
#new_record? ⇒ Boolean
Returns true if this record has not been persisted to SimpleDB.
-
#persisted? ⇒ Boolean
Persistence indicates if the record has been saved previously or not.
-
#save ⇒ Boolean
Creates new records, updates existing records.
-
#save! ⇒ true
Creates new records, updates exsting records.
-
#shard ⇒ String
(also: #domain)
Returns the name of the shard this record is persisted to or will be persisted to.
-
#update_attributes(attribute_hash) ⇒ Boolean
Bulk assigns the attributes and then saves the record.
-
#update_attributes!(attribute_hash) ⇒ true
Bulk assigns the attributes and then saves the record.
-
#valid? ⇒ Boolean
Returns true if this record has no validation errors.
Instance Method Details
#attributes ⇒ Hash
Returns A hash with attribute names as hash keys (strings) and attribute values (of mixed types) as hash values.
96 97 98 99 100 101 102 |
# File 'lib/aws/record/abstract_base.rb', line 96 def attributes attributes = Core::IndifferentHash.new attributes['id'] = id if persisted? self.class.attributes.keys.inject(attributes) do |hash,attr_name| hash.merge(attr_name => __send__(attr_name)) end end |
#attributes=(attributes) ⇒ Hash
Acts like #update but does not call #save.
record.attributes = { :name => 'abc', :age => 20 }
113 114 115 |
# File 'lib/aws/record/abstract_base.rb', line 113 def attributes= attributes bulk_assign(attributes) end |
#delete ⇒ true
Deletes the record.
193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/aws/record/abstract_base.rb', line 193 def delete if persisted? if deleted? raise 'unable to delete, this object has already been deleted' else delete_storage @_deleted = true end else raise 'unable to delete, this object has not been saved yet' end end |
#deleted? ⇒ Boolean
Returns true if this instance object has been deleted.
207 208 209 |
# File 'lib/aws/record/abstract_base.rb', line 207 def deleted? persisted? ? !!@_deleted : false end |
#errors ⇒ Object
142 143 144 |
# File 'lib/aws/record/abstract_base.rb', line 142 def errors @errors ||= Errors.new end |
#id ⇒ String
The id for each record is auto-generated. The default strategy generates uuid strings.
90 91 92 |
# File 'lib/aws/record/abstract_base.rb', line 90 def id @_id end |
#initialize(attributes = {}) ⇒ Model, HashModel
Constructs a new record.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/aws/record/abstract_base.rb', line 61 def initialize attributes = {} attributes = attributes.dup # supporting :domain for backwards compatability, :shard is prefered @_shard = attributes.delete(:domain) @_shard ||= attributes.delete('domain') @_shard ||= attributes.delete(:shard) @_shard ||= attributes.delete('shard') @_shard = self.class.shard_name(@_shard) @_data = {} assign_default_values bulk_assign(attributes) end |
#new_record? ⇒ Boolean
Returns true if this record has not been persisted to SimpleDB.
132 133 134 |
# File 'lib/aws/record/abstract_base.rb', line 132 def new_record? !persisted? end |
#persisted? ⇒ Boolean
Persistence indicates if the record has been saved previously or not.
126 127 128 |
# File 'lib/aws/record/abstract_base.rb', line 126 def persisted? !!@_persisted end |
#save ⇒ Boolean
Creates new records, updates existing records.
149 150 151 152 153 154 155 156 157 |
# File 'lib/aws/record/abstract_base.rb', line 149 def save if valid? persisted? ? update : create clear_changes! true else false end end |
#save! ⇒ true
Creates new records, updates exsting records. If there is a validation error then an exception is raised.
164 165 166 167 |
# File 'lib/aws/record/abstract_base.rb', line 164 def save! raise InvalidRecordError.new(self) unless save true end |
#shard ⇒ String Also known as: domain
Returns the name of the shard this record is persisted to or will be persisted to. Defaults to the domain/table named after this record class.
81 82 83 |
# File 'lib/aws/record/abstract_base.rb', line 81 def shard @_shard end |
#update_attributes(attribute_hash) ⇒ Boolean
Bulk assigns the attributes and then saves the record.
173 174 175 176 |
# File 'lib/aws/record/abstract_base.rb', line 173 def update_attributes attribute_hash bulk_assign(attribute_hash) save end |
#update_attributes!(attribute_hash) ⇒ true
Bulk assigns the attributes and then saves the record. Raises an exception (AWS::Record::InvalidRecordError) if the record is not valid.
183 184 185 186 187 188 189 |
# File 'lib/aws/record/abstract_base.rb', line 183 def update_attributes! attribute_hash if update_attributes(attribute_hash) true else raise InvalidRecordError.new(self) end end |
#valid? ⇒ Boolean
Returns true if this record has no validation errors.
137 138 139 140 |
# File 'lib/aws/record/abstract_base.rb', line 137 def valid? run_validations errors.empty? end |