File: dirty_tracking.rb

package info (click to toggle)
ruby-aws-sdk 1.67.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,840 kB
  • sloc: ruby: 28,436; makefile: 7
file content (287 lines) | stat: -rw-r--r-- 8,848 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  module Record

    # Provides a way to track changes in your records.
    #
    #     my_book = Book['bookid']
    #
    #     my_book.changed? #=> false
    #     my_book.title #=> "My Book"
    #     my_book.title = "My Awesome Book"
    #     my_book.changed? #=> true
    #
    #     my_book = Book['bookid']
    #
    # You can inspect further and get a list of changed attributes
    #
    #     my_book.changed #=> ['title']
    #
    # Or you can get a more detailed description of the changes.  {#changes}
    # returns a hash of changed attributes (keys) with their old and new
    # values.
    #
    #     my_book.changes
    #     #=> { 'title' => ['My Book', 'My Awesome Book']
    #
    # For every configured attribute you also get a handful of methods
    # for inspecting changes on that attribute.  Given the following
    # attribute:
    #
    #     string_attr :title
    #
    # You can now call any of the following methods:
    #
    #   * title_changed?
    #   * title_change
    #   * title_was
    #   * reset_title!
    #   * title_will_change!
    #
    # Given the title change from above:
    #
    #     my_book.title_changed? #=> true
    #     my_book.title_change #=> ['My Book', 'My Awesome Book']
    #     my_book.title_was #=> ['My Book']
    #
    #     my_book.reset_title!
    #     my_book.title #=> 'My Book'
    #
    # ## In-Place Editing
    #
    # Dirty tracking works by comparing incoming attribute values upon
    # assignment against the value that was there previously.  If you
    # use functions against the value that modify it (like gsub!)
    # you must notify your record about the coming change.
    #
    #     my_book.title #=> 'My Book'
    #     my_book.title_will_change!
    #     my_book.title.gsub!(/My/, 'Your')
    #     my_book.title_change #=> ['My Book', 'Your Book']
    #
    # ## Partial Updates
    #
    # Dirty tracking makes it possible to only persist those attributes
    # that have changed since they were loaded.  This speeds up requests
    # against AWS when saving data.
    #
    module DirtyTracking

      # Returns true if this model has unsaved changes.
      #
      #     b = Book.new(:title => 'My Book')
      #     b.changed?
      #     #=> true
      #
      # New objects and objects freshly loaded should not have any changes:
      #
      #     b = Book.new
      #     b.changed?      #=> false
      #
      #     b = Book.first
      #     b.changed?      #=> false
      #
      # @return [Boolean] Returns true if any of the attributes have
      #   unsaved changes.
      def changed?
        !orig_values.empty?
      end

      # Returns an array of attribute names that have changes.
      #
      #     book.changed #=> []
      #     person.title = 'New Title'
      #     book.changed #=> ['title']
      #
      # @return [Array] Returns an array of attribute names that have
      #   unsaved changes.
      def changed
        orig_values.keys
      end

      # Returns the changed attributes in a hash.  Keys are attribute names,
      # values are two value arrays.  The first value is the previous
      # attribute value, the second is the current attribute value.
      #
      #     book.title = 'New Title'
      #     book.changes
      #     #=> { 'title' => ['Old Title', 'New Title'] }
      #
      # @return [Hash] Returns a hash of attribute changes.
      def changes
        changed.inject({}) do |changes, attr_name|
          changes[attr_name] = attribute_change(attr_name)
          changes
        end
      end

      # Returns true if the named attribute has unsaved changes.
      #
      # This is an attribute method.  The following two expressions
      # are equivilent:
      #
      #     book.title_changed?
      #     book.attribute_changed?(:title)
      #
      # @param [String] attribute_name Name of the attribute to check
      #   for changes.
      #
      # @return [Boolean] Returns true if the named attribute
      #   has unsaved changes.
      # @api private
      private
      def attribute_changed? attribute_name
        orig_values.keys.include?(attribute_name)
      end

      # Returns an array of the old value and the new value for
      # attributes that have unsaved changes, returns nil otherwise.
      #
      # This is an attribute method.  The following two expressions
      # are equivilent:
      #
      #     book.title_change
      #     book.attribute_change(:title)
      #
      # @example Asking for changes on an unchanged attribute
      #
      #   book = Book.new
      #   book.title_change #=> nil
      #
      # @example Getting changed attributes on a new object
      #
      #   book = Book.new(:title => 'My Book')
      #   book.title_change #=> [nil, 'My Book']
      #
      # @example Getting changed attributes on a loaded object
      #
      #   book = Book.first
      #   book.title = 'New Title'
      #   book.title_change #=> ['Old Title', 'New Title']
      #
      # @param [String] attribute_name Name of the attribute to fetch
      #   a change for.
      # @return [Boolean] Returns true if the named attribute
      #   has unsaved changes.
      # @api private
      private
      def attribute_change attribute_name
        self.class.attribute_for(attribute_name) do |attribute|
          if orig_values.has_key?(attribute.name)
            [orig_values[attribute.name], __send__(attribute.name)]
          else
            nil
          end
        end
      end

      # Returns the previous value for changed attributes, or the current
      # value for unchanged attributes.
      #
      # This is an attribute method.  The following two expressions
      # are equivilent:
      #
      #      book.title_was
      #      book.attribute_was(:title)
      #
      # @example Returns the previous value for changed attributes:
      #
      #   book = Book.where(:title => 'My Book').first
      #   book.title = 'New Title'
      #   book.title_was #=> 'My Book'
      #
      # @example Returns the current value for unchanged attributes:
      #
      #   book = Book.where(:title => 'My Book').first
      #   book.title_was #=> 'My Book'
      #
      # @return Returns the previous value for changed attributes
      #   or the current value for unchanged attributes.
      # @api private
      private
      def attribute_was attribute_name
        self.class.attribute_for(attribute_name) do |attribute|
          name = attribute.name
          orig_values.has_key?(name) ? orig_values[name] : __send__(name)
        end
      end

      # Reverts any changes to the attribute, restoring its original value.
      # @param [String] attribute_name Name of the attribute to reset.
      # @return [nil]
      # @api private
      private
      def reset_attribute! attribute_name
        __send__("#{attribute_name}=", attribute_was(attribute_name))
        nil
      end

      # Indicate to the record that you are about to edit an attribute
      # in place.
      # @param [String] attribute_name Name of the attribute that will
      #   be changed.
      # @return [nil]
      # @api private
      private
      def attribute_will_change! attribute_name
        self.class.attribute_for(attribute_name) do |attribute|
          name = attribute.name
          unless orig_values.has_key?(name)
            was = __send__(name)
            begin
              # booleans, nil, etc all #respond_to?(:clone), but they raise
              # a TypeError when you attempt to dup them.
              orig_values[name] = was.clone
            rescue TypeError
              orig_values[name] = was
            end
          end
        end
        nil
      end

      private
      def orig_values
        @_orig_values ||= {}
      end

      private
      def clear_change! attribute_name
        orig_values.delete(attribute_name)
      end

      private
      def ignore_changes &block
        begin
          @_ignore_changes = true
          yield
        ensure
          @_ignore_changes = false
        end
      end

      private
      def if_tracking_changes &block
        yield unless @_ignore_changes
      end

      private
      def clear_changes!
        orig_values.clear
      end

    end
  end
end