Module: AWS::Record::AbstractBase::InstanceMethods

Defined in:
lib/aws/record/abstract_base.rb

Instance Method Summary collapse

Instance Method Details

#attributesHash



93
94
95
96
97
98
99
# File 'lib/aws/record/abstract_base.rb', line 93

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 }


110
111
112
# File 'lib/aws/record/abstract_base.rb', line 110

def attributes= attributes
  bulk_assign(attributes)
end

#deletetrue Also known as: destroy

Deletes the record.



190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/aws/record/abstract_base.rb', line 190

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



205
206
207
# File 'lib/aws/record/abstract_base.rb', line 205

def deleted?
  persisted? ? !!@_deleted : false
end

#errorsObject



139
140
141
# File 'lib/aws/record/abstract_base.rb', line 139

def errors
  @errors ||= Errors.new
end

#idString

The id for each record is auto-generated. The default strategy generates uuid strings.



87
88
89
# File 'lib/aws/record/abstract_base.rb', line 87

def id
  @_id
end

#initialize(attributes = {}) ⇒ Model, HashModel

Constructs a new record.

Options Hash (attributes):

  • :shard (String)

    The domain/table this record should persist to. If this is omitted, it will persist to the class default shard (which defaults to the class name).



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aws/record/abstract_base.rb', line 58

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



129
130
131
# File 'lib/aws/record/abstract_base.rb', line 129

def new_record?
  !persisted?
end

#persisted?Boolean

Persistence indicates if the record has been saved previously or not.

Examples:

@recipe = Recipe.new(:name => 'Buttermilk Pancackes')
@recipe.persisted? #=> false
@recipe.save!
@recipe.persisted? #=> true


123
124
125
# File 'lib/aws/record/abstract_base.rb', line 123

def persisted?
  !!@_persisted
end

#saveBoolean

Creates new records, updates existing records.



146
147
148
149
150
151
152
153
154
# File 'lib/aws/record/abstract_base.rb', line 146

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.

Raises:

  • (InvalidRecordError)

    Raised when the record has validation errors and can not be saved.



161
162
163
164
# File 'lib/aws/record/abstract_base.rb', line 161

def save!
  raise InvalidRecordError.new(self) unless save
  true
end

#shardString Also known as: domain



78
79
80
# File 'lib/aws/record/abstract_base.rb', line 78

def shard
  @_shard
end

#update_attributes(attribute_hash) ⇒ Boolean

Bulk assigns the attributes and then saves the record.



170
171
172
173
# File 'lib/aws/record/abstract_base.rb', line 170

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.



180
181
182
183
184
185
186
# File 'lib/aws/record/abstract_base.rb', line 180

def update_attributes! attribute_hash
  if update_attributes(attribute_hash)
    true
  else
    raise InvalidRecordError.new(self)
  end
end

#valid?Boolean



134
135
136
137
# File 'lib/aws/record/abstract_base.rb', line 134

def valid?
  run_validations
  errors.empty?
end