class Aws::Record::ItemData

@api private

Attributes

destroyed[RW]
new_record[RW]

Public Class Methods

new(model_attributes, opts) click to toggle source
# File lib/aws-record/record/item_data.rb, line 19
def initialize(model_attributes, opts)
  @data = {}
  @clean_copies = {}
  @dirty_flags = {}
  @model_attributes = model_attributes
  @track_mutations = opts[:track_mutations]
  @track_mutations = true if opts[:track_mutations].nil?
  @new_record = true
  @destroyed = false

  populate_default_values
end

Public Instance Methods

attribute_dirty!(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 84
def attribute_dirty!(name)
  @dirty_flags[name] = true
end
attribute_dirty?(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 71
def attribute_dirty?(name)
  if @dirty_flags[name]
    true
  else
    value = get_attribute(name)
    value != @clean_copies[name]
  end
end
attribute_was(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 80
def attribute_was(name)
  @clean_copies[name]
end
build_save_hash() click to toggle source
# File lib/aws-record/record/item_data.rb, line 111
def build_save_hash
  @data.inject({}) do |acc, name_value_pair|
    attr_name, raw_value = name_value_pair
    attribute = @model_attributes.attribute_for(attr_name)
    if !raw_value.nil? || attribute.persist_nil?
      db_name = attribute.database_name
      acc[db_name] = attribute.serialize(raw_value)
    end
    acc
  end
end
clean!() click to toggle source
# File lib/aws-record/record/item_data.rb, line 58
def clean!
  @dirty_flags = {}
  @model_attributes.attributes.each_key do |name|
    populate_default_values
    value = get_attribute(name)
    if @track_mutations
      @clean_copies[name] = _deep_copy(value)
    else
      @clean_copies[name] = value
    end
  end
end
destroyed?() click to toggle source
# File lib/aws-record/record/item_data.rb, line 46
def destroyed?
  @destroyed
end
dirty() click to toggle source
# File lib/aws-record/record/item_data.rb, line 88
def dirty
  @model_attributes.attributes.keys.inject([]) do |acc, name|
    acc << name if attribute_dirty?(name)
    acc
  end
end
dirty?() click to toggle source
# File lib/aws-record/record/item_data.rb, line 95
def dirty?
  dirty.empty? ? false : true
end
get_attribute(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 34
def get_attribute(name)
  @model_attributes.attribute_for(name).type_cast(@data[name])
end
hash_copy() click to toggle source
# File lib/aws-record/record/item_data.rb, line 107
def hash_copy
  @data.dup
end
new_record?() click to toggle source
# File lib/aws-record/record/item_data.rb, line 42
def new_record?
  @new_record
end
persisted?() click to toggle source
# File lib/aws-record/record/item_data.rb, line 50
def persisted?
  !(new_record? || destroyed?)
end
populate_default_values() click to toggle source
# File lib/aws-record/record/item_data.rb, line 123
def populate_default_values
  @model_attributes.attributes.each do |name, attribute|
    unless (default_value = attribute.default_value).nil?
      if @data[name].nil? && @data[name].nil?
        @data[name] = default_value
      end
    end
  end
end
raw_value(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 54
def raw_value(name)
  @data[name]
end
rollback_attribute!(name) click to toggle source
# File lib/aws-record/record/item_data.rb, line 99
def rollback_attribute!(name)
  if attribute_dirty?(name)
    @dirty_flags.delete(name)
    set_attribute(name, attribute_was(name))
  end
  get_attribute(name)
end
set_attribute(name, value) click to toggle source
# File lib/aws-record/record/item_data.rb, line 38
def set_attribute(name, value)
  @data[name] = value
end

Private Instance Methods

_deep_copy(obj) click to toggle source
# File lib/aws-record/record/item_data.rb, line 134
def _deep_copy(obj)
  Marshal.load(Marshal.dump(obj))
end