You are viewing documentation for version 1 of the AWS SDK for Ruby. Version 2 documentation can be found here.
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
(also: #destroy)
Deletes the record.
-
#deleted? ⇒ Boolean
Returns true if this instance object has been deleted.
-
#errors ⇒ Object
-
#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(opts = {}) ⇒ 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?(opts = {}) ⇒ 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.
76 77 78 79 80 81 |
# File 'lib/aws/record/abstract_base.rb', line 76 def attributes attributes = Core::IndifferentHash.new 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 }
92 93 94 |
# File 'lib/aws/record/abstract_base.rb', line 92 def attributes= attributes bulk_assign(attributes) end |
#delete ⇒ true Also known as: destroy
Deletes the record.
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/aws/record/abstract_base.rb', line 176 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.
191 192 193 |
# File 'lib/aws/record/abstract_base.rb', line 191 def deleted? persisted? ? !!@_deleted : false end |
#errors ⇒ Object
[View source]
124 125 126 |
# File 'lib/aws/record/abstract_base.rb', line 124 def errors @errors ||= Errors.new end |
#initialize(attributes = {}) ⇒ Model, HashModel
Constructs a new record.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/aws/record/abstract_base.rb', line 49 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.
111 112 113 |
# File 'lib/aws/record/abstract_base.rb', line 111 def new_record? !persisted? end |
#persisted? ⇒ Boolean
Persistence indicates if the record has been saved previously or not.
105 106 107 |
# File 'lib/aws/record/abstract_base.rb', line 105 def persisted? !!@_persisted end |
#save(opts = {}) ⇒ Boolean
Creates new records, updates existing records.
132 133 134 135 136 137 138 139 140 |
# File 'lib/aws/record/abstract_base.rb', line 132 def save opts = {} if valid?(opts) 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.
147 148 149 150 |
# File 'lib/aws/record/abstract_base.rb', line 147 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.
69 70 71 |
# File 'lib/aws/record/abstract_base.rb', line 69 def shard @_shard end |
#update_attributes(attribute_hash) ⇒ Boolean
Bulk assigns the attributes and then saves the record.
156 157 158 159 |
# File 'lib/aws/record/abstract_base.rb', line 156 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.
166 167 168 169 170 171 172 |
# File 'lib/aws/record/abstract_base.rb', line 166 def update_attributes! attribute_hash if update_attributes(attribute_hash) true else raise InvalidRecordError.new(self) end end |
#valid?(opts = {}) ⇒ Boolean
Returns true if this record has no validation errors.
117 118 119 120 121 122 |
# File 'lib/aws/record/abstract_base.rb', line 117 def valid? opts = {} opts = {} if opts.nil? opts = {:validate => true}.merge(opts) run_validations if opts[:validate] errors.empty? end |