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

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

Instance Method Summary collapse

Instance Method Details

#attributesHash

Returns A hash with attribute names as hash keys (strings) and attribute values (of mixed types) as hash values.

Returns:

  • (Hash)

    A hash with attribute names as hash keys (strings) and attribute values (of mixed types) as hash values.



85
86
87
88
89
90
91
# File 'lib/aws/record/abstract_base.rb', line 85

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 }

Parameters:

  • attributes (Hash)

    A hash of attributes to set on this record without calling save.

Returns:

  • (Hash)

    Returns the attribute hash that was passed in.



102
103
104
# File 'lib/aws/record/abstract_base.rb', line 102

def attributes= attributes
  bulk_assign(attributes)
end

#deletetrue Also known as: destroy

Deletes the record.

Returns:

  • (true)


186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/aws/record/abstract_base.rb', line 186

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.

Returns:

  • (Boolean)

    Returns true if this instance object has been deleted.



201
202
203
# File 'lib/aws/record/abstract_base.rb', line 201

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

#errorsObject



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

def errors
  @errors ||= Errors.new
end

#idString

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

Returns:

  • (String)

    Returns the id string (uuid) for this record. Retuns nil if this is a new record that has not been persisted yet.



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

def id
  @_id
end

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

Constructs a new record.

Parameters:

  • attributes (Hash) (defaults to: {})

    Attributes that should be bulk assigned to this record. You can also specify the shard (i.e. domain or table) this record should persist to via :shard).

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).

Returns:

  • (Model, HashModel)

    Returns a new (non-persisted) record. Call #save to persist changes to AWS.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/aws/record/abstract_base.rb', line 50

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.

Returns:

  • (Boolean)

    Returns true if this record has not been persisted to SimpleDB.



121
122
123
# File 'lib/aws/record/abstract_base.rb', line 121

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

Returns:

  • (Boolean)

    Returns true if this record has been persisted.



115
116
117
# File 'lib/aws/record/abstract_base.rb', line 115

def persisted?
  !!@_persisted
end

#save(opts = {}) ⇒ Boolean

Creates new records, updates existing records.

Parameters:

  • opts (Hash) (defaults to: {})

    Pass :validate => false to skip validations

Returns:

  • (Boolean)

    Returns true if the record saved without errors, false otherwise.



142
143
144
145
146
147
148
149
150
# File 'lib/aws/record/abstract_base.rb', line 142

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.

Returns:

  • (true)

    Returns true after a successful save.

Raises:

  • (InvalidRecordError)

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



157
158
159
160
# File 'lib/aws/record/abstract_base.rb', line 157

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

#shardString 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.

Returns:

  • (String)

    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.



70
71
72
# File 'lib/aws/record/abstract_base.rb', line 70

def shard
  @_shard
end

#update_attributes(attribute_hash) ⇒ Boolean

Bulk assigns the attributes and then saves the record.

Parameters:

  • attribute_hash (Hash)

    A hash of attribute names (keys) and attribute values to assign to this record.

Returns:

  • (Boolean)

    Returns true if the record saved without errors, false otherwise.



166
167
168
169
# File 'lib/aws/record/abstract_base.rb', line 166

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.

Parameters:

  • attribute_hash (Hash)

    A hash of attribute names (keys) and attribute values to assign to this record.

Returns:

  • (true)


176
177
178
179
180
181
182
# File 'lib/aws/record/abstract_base.rb', line 176

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.

Parameters:

  • opts (Hash) (defaults to: {})

    Pass :validate => false to skip validations

Returns:

  • (Boolean)

    Returns true if this record has no validation errors.



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

def valid? opts = {}
  opts = {} if opts.nil?
  opts = {:validate => true}.merge(opts)
  run_validations if opts[:validate]
  errors.empty?
end