Module: Aws::Record::DirtyTracking

Included in:
Aws::Record
Defined in:
lib/aws-record/record/dirty_tracking.rb

Defined Under Namespace

Modules: DirtyTrackingClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(sub_class) ⇒ Object



6
7
8
# File 'lib/aws-record/record/dirty_tracking.rb', line 6

def self.included(sub_class)
  sub_class.extend(DirtyTrackingClassMethods)
end

Instance Method Details

#attribute_dirty!(name) ⇒ Object

Mark that an attribute is changing. This is useful in situations where it is necessary to track that the value of an attribute is changing in-place.

Examples:

class Model
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name
end

model.name        # => 'Alex'
model.name_dirty? # => false
model.name_was    # => 'Alex'

model.name << 'i'
model.name        # => 'Alexi'

# The change was made in place. Since the String instance representing
# the value of name is the same as it was originally, the change is not
# detected.
model.name_dirty? # => false
model.name_was    # => 'Alexi'

model.name_dirty!
model.name_dirty? # => true
model.name_was    # => 'Alexi'

model.name << 's'
model.name        # => 'Alexis'
model.name_dirty? # => true
model.name_was    # => 'Alexi'

Parameters:

  • name (String, Symbol)

    The name of the attribute to mark as changing.



84
85
86
# File 'lib/aws-record/record/dirty_tracking.rb', line 84

def attribute_dirty!(name)
  @data.attribute_dirty!(name)
end

#attribute_dirty?(name) ⇒ Boolean

Returns true if the specified attribute has any dirty changes, false otherwise.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model.name_dirty? # => false
model.name     # => 'Alex'
model.name = 'Nick'
model.name_dirty? # => true

Parameters:

  • name (String, Symbol)

    The name of the attribute to to check for dirty changes.

Returns:

  • (Boolean)

    true if the specified attribute has any dirty changes, false otherwise.



26
27
28
# File 'lib/aws-record/record/dirty_tracking.rb', line 26

def attribute_dirty?(name)
  @data.attribute_dirty?(name)
end

#attribute_was(name) ⇒ Object

Returns the original value of the specified attribute.

Examples:

class Model
  include Aws::Record
  integer_attr :id,   hash_key: true
  string_attr  :name
end

model.name         # => 'Alex'
model.name = 'Nick'
model.name_was     # => 'Alex'

Parameters:

  • name (String, Symbol)

    The name of the attribute to retrieve the original value of.

Returns:

  • (Object)

    The original value of the specified attribute.



45
46
47
# File 'lib/aws-record/record/dirty_tracking.rb', line 45

def attribute_was(name)
  @data.attribute_was(name)
end

#destroyed?Boolean

Returns true if the model has been destroyed, false otherwise.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model = Model.new
model.destroyed? # => false
model.save
model.destroyed? # => false
model.delete!
model.destroyed? # => true

Returns:

  • (Boolean)

    true if the model has been destroyed, false otherwise.



187
188
189
# File 'lib/aws-record/record/dirty_tracking.rb', line 187

def destroyed?
  @data.destroyed?
end

#dirtyArray

Returns an array with the name of the attributes with dirty changes.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model.dirty # => []
model.name  # => 'Alex'
model.name = 'Nick'
model.dirty # => ['name']

Returns:

  • (Array)

    The names of attributes with dirty changes.



103
104
105
# File 'lib/aws-record/record/dirty_tracking.rb', line 103

def dirty
  @data.dirty
end

#dirty?Boolean

Returns true if any attributes have dirty changes, false otherwise.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model.dirty? # => false
model.name   # => 'Alex'
model.name = 'Nick'
model.dirty? # => true

Returns:

  • (Boolean)

    true if any attributes have dirty changes, false otherwise.



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

def dirty?
  @data.dirty?
end

#new_record?Boolean

Returns true if the model is newly initialized, false otherwise.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model = Model.new
model.new_record? # => true
model.save
model.new_record? # => false

Returns:

  • (Boolean)

    true if the model is newly initialized, false otherwise.



165
166
167
# File 'lib/aws-record/record/dirty_tracking.rb', line 165

def new_record?
  @data.new_record?
end

#persisted?Boolean

Returns true if the model is not new and has not been deleted, false otherwise.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model = Model.new
model.persisted? # => false
model.save
model.persisted? # => true
model.delete!
model.persisted? # => false

Returns:

  • (Boolean)

    true if the model is not new and has not been deleted, false otherwise.



145
146
147
# File 'lib/aws-record/record/dirty_tracking.rb', line 145

def persisted?
  @data.persisted?
end

#reload!self

Returns the item instance.

Returns:

  • (self)

    Returns the item instance.

Raises:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/aws-record/record/dirty_tracking.rb', line 198

def reload!
  primary_key = self.class.keys.values.each_with_object({}) do |key, memo|
    memo[key] = send(key)
    memo
  end

  record = self.class.find(primary_key)

  raise Errors::NotFound, 'No record found' unless record.present?

  @data = record.instance_variable_get('@data')

  clean!

  self
end

#rollback!(names = dirty) ⇒ Object

Restores all attributes to their original values.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model.name # => 'Alex'
model.name = 'Nick'
model.rollback!
model.name # => 'Alex'

Parameters:

  • names (Array, String, Symbol) (defaults to: dirty)

    The names of attributes to restore.



249
250
251
# File 'lib/aws-record/record/dirty_tracking.rb', line 249

def rollback!(names = dirty)
  Array(names).each { |name| rollback_attribute!(name) }
end

#rollback_attribute!(name) ⇒ Object

Restores the attribute specified to its original value.

Examples:

class Model
  include Aws::Record
  integer_attr :id, hash_key: true
  string_attr  :name
end

model.name # => 'Alex'
model.name = 'Nick'
model.rollback_attribute!(:name)
model.name # => 'Alex'

Parameters:

  • name (String, Symbol)

    The name of the attribute to restore



230
231
232
# File 'lib/aws-record/record/dirty_tracking.rb', line 230

def rollback_attribute!(name)
  @data.rollback_attribute!(name)
end

#saveObject



259
260
261
# File 'lib/aws-record/record/dirty_tracking.rb', line 259

def save(*)
  super.tap { clean! }
end