Class: Aws::Record::ItemData Private

Inherits:
Object
  • Object
show all
Defined in:
lib/aws-record/record/item_data.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_attributes, opts) ⇒ ItemData

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ItemData.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/aws-record/record/item_data.rb', line 8

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

Instance Attribute Details

#destroyedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/aws-record/record/item_data.rb', line 21

def destroyed
  @destroyed
end

#new_recordObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



21
22
23
# File 'lib/aws-record/record/item_data.rb', line 21

def new_record
  @new_record
end

Instance Method Details

#attribute_dirty!(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
# File 'lib/aws-record/record/item_data.rb', line 73

def attribute_dirty!(name)
  @dirty_flags[name] = true
end

#attribute_dirty?(name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
# File 'lib/aws-record/record/item_data.rb', line 60

def attribute_dirty?(name)
  if @dirty_flags[name]
    true
  else
    value = get_attribute(name)
    value != @clean_copies[name]
  end
end

#attribute_was(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



69
70
71
# File 'lib/aws-record/record/item_data.rb', line 69

def attribute_was(name)
  @clean_copies[name]
end

#build_save_hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/aws-record/record/item_data.rb', line 100

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!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aws-record/record/item_data.rb', line 47

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?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


35
36
37
# File 'lib/aws-record/record/item_data.rb', line 35

def destroyed?
  @destroyed
end

#dirtyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



77
78
79
80
81
82
# File 'lib/aws-record/record/item_data.rb', line 77

def dirty
  @model_attributes.attributes.keys.inject([]) do |acc, name|
    acc << name if attribute_dirty?(name)
    acc
  end
end

#dirty?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


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

def dirty?
  dirty.empty? ? false : true
end

#get_attribute(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



23
24
25
# File 'lib/aws-record/record/item_data.rb', line 23

def get_attribute(name)
  @model_attributes.attribute_for(name).type_cast(@data[name])
end

#hash_copyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
# File 'lib/aws-record/record/item_data.rb', line 96

def hash_copy
  @data.dup
end

#new_record?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


31
32
33
# File 'lib/aws-record/record/item_data.rb', line 31

def new_record?
  @new_record
end

#persisted?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


39
40
41
# File 'lib/aws-record/record/item_data.rb', line 39

def persisted?
  !(new_record? || destroyed?)
end

#populate_default_valuesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



112
113
114
115
116
117
118
119
120
# File 'lib/aws-record/record/item_data.rb', line 112

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/aws-record/record/item_data.rb', line 43

def raw_value(name)
  @data[name]
end

#rollback_attribute!(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



88
89
90
91
92
93
94
# File 'lib/aws-record/record/item_data.rb', line 88

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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



27
28
29
# File 'lib/aws-record/record/item_data.rb', line 27

def set_attribute(name, value)
  @data[name] = value
end