File: attributes.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 (377 lines) | stat: -rw-r--r-- 13,222 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# 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
    class Model

      module Attributes

        class BooleanAttr < Record::Attributes::BooleanAttr
          def self.serialize boolean, options = {}
            super.to_s
          end
        end

        class IntegerAttr < Record::Attributes::IntegerAttr
          def self.serialize integer, options = {}
            super.to_s
          end
        end

        class FloatAttr < Record::Attributes::FloatAttr
          def self.serialize float, options = {}
            super.to_s
          end
        end

        class SortableIntegerAttr < IntegerAttr

          def initialize name, options = {}
            range = options[:range]
            raise ArgumentError, "missing required option :range" unless range
            raise ArgumentError, ":range should be a integer range" unless
              range.is_a?(Range) and range.first.is_a?(Integer)
            super(name, options)
          end

          # Returns a serialized representation of the integer value suitable for
          # storing in SimpleDB.
          #
          #     attribute.serialize(123)
          #     #=> '123'
          #
          #     # padded to the correct number of digits
          #     attribute.serialize('123', :range => (0..10_000)
          #     #=> '00123'
          #
          #     # offset applied to make all values positive
          #     attribute.serialize('-55', :range => (-100..10_000)
          #     #=> '00045'
          #
          # @param [Integer] integer The number to serialize.
          # @param [Hash] options
          # @option options [required,Range] :range A range that represents the
          #   minimum and maximum values this integer can be.
          #   The returned value will have an offset applied (if min is
          #   less than 0) and will be zero padded.
          # @return [String] A serialized representation of the integer.
          def self.serialize integer, options = {}
            expect(Integer, integer) do
              check_range(integer, options)
              offset_and_precision(options) do |offset,precision|
                "%0#{precision}d" % (integer.to_i + offset)
              end
            end
          end

          def self.deserialize string_value, options = {}
            offset_and_precision(options) do |offset,precision|
              string_value.to_i - offset
            end
          end

          protected
          def self.offset_and_precision options, &block

            min = options[:range].first
            max = options[:range].last

            offset = min < 0 ? min * -1 : 0
            precision = (max + offset).to_s.length

            yield(offset, precision)

          end

          def self.check_range number, options
            unless options[:range].include?(number)
              msg = "unable to serialize `#{number}`, falls outside " +
               "the range #{options[:range]}"
              raise msg
            end
          end

        end

        class SortableFloatAttr < FloatAttr

          def initialize name, options = {}
            range = options[:range]
            raise ArgumentError, "missing required option :range" unless range
            raise ArgumentError, ":range should be an integer range" unless
              range.is_a?(Range) and range.first.is_a?(Integer)
            super(name, options)
          end

          def self.serialize float, options = {}
            expect(Float, float) do
              left, right = float.to_s.split('.')
              left = SortableIntegerAttr.serialize(left.to_i, options)
              SortableIntegerAttr.check_range(float, options)
              "#{left}.#{right}"
            end
          end

          def self.deserialize string_value, options = {}
            expect(String, string_value) do
              left, right = string_value.split('.')
              left = SortableIntegerAttr.deserialize(left, options)
              "#{left}.#{right}".to_f
            end
          end

        end

      end

      class << self

        # Adds a string attribute to this class.
        #
        # @example A standard string attribute
        #
        #   class Recipe < AWS::Record::Model
        #     string_attr :name
        #   end
        #
        #   recipe = Recipe.new(:name => "Buttermilk Pancakes")
        #   recipe.name #=> 'Buttermilk Pancakes'
        #
        # @example A string attribute with `:set` set to true
        #
        #   class Recipe < AWS::Record::Model
        #     string_attr :tags, :set => true
        #   end
        #
        #   recipe = Recipe.new(:tags => %w(popular dessert))
        #   recipe.tags #=> #<Set: {"popular", "desert"}>
        #
        # @param [Symbol] name The name of the attribute.
        # @param [Hash] options
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple values.
        def string_attr name, options = {}
          add_attribute(Record::Attributes::StringAttr.new(name, options))
        end

        # Adds an integer attribute to this class.
        #
        #     class Recipe < AWS::Record::Model
        #       integer_attr :servings
        #     end
        #
        #     recipe = Recipe.new(:servings => '10')
        #     recipe.servings #=> 10
        #
        # @param [Symbol] name The name of the attribute.
        # @param [Hash] options
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple values.
        def integer_attr name, options = {}
          add_attribute(Attributes::IntegerAttr.new(name, options))
        end

        # Adds a sortable integer attribute to this class.
        #
        #     class Person < AWS::Record::Model
        #       sortable_integer_attr :age, :range => 0..150
        #     end
        #
        #     person = Person.new(:age => 10)
        #     person.age #=> 10
        #
        # ### Validations
        #
        # It is recomended to apply a validates_numericality_of with
        # minimum and maximum value constraints.  If a value is assigned
        # to a sortable integer that falls outside of the +:range: it will
        # raise a runtime error when the record is saved.
        #
        # ### Difference Between Sortable an Regular Integer Attributes
        #
        # Because SimpleDB does not support numeric types, all values must
        # be converted to strings.  This complicates sorting by numeric values.
        # To accomplish sorting numeric attributes the values must be
        # zero padded and have an offset applied to eliminate negative values.
        #
        # @param [Symbol] name The name of the attribute.
        # @param [Hash] options
        # @option options [Range] :range A numeric range the represents the
        #   minimum and  maximum values this attribute should accept.
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple values.
        def sortable_integer_attr name, options = {}
          add_attribute(Attributes::SortableIntegerAttr.new(name, options))
        end

        # Adds a float attribute to this class.
        #
        #     class Listing < AWS::Record::Model
        #       float_attr :score
        #     end
        #
        #     listing = Listing.new(:score => '123.456')
        #     listing.score # => 123.456
        #
        # @param [Symbol] name The name of the attribute.
        # @param [Hash] options
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple values.
        def float_attr name, options = {}
          add_attribute(Attributes::FloatAttr.new(name, options))
        end

        # Adds sortable float attribute to this class.
        #
        # Persisted values are stored (and sorted) as strings.  This makes it
        # more difficult to sort numbers because they don't sort
        # lexicographically unless they have been offset to be positive and
        # then zero padded.
        #
        # ### Postive Floats
        #
        # To store floats in a sort-friendly manor:
        #
        #     sortable_float_attr :score, :range => (0..10)
        #
        # This will cause values like 5.5 to persist as a string like '05.5' so
        # that they can be sorted lexicographically.
        #
        # ### Negative Floats
        #
        # If you need to store negative sortable floats, increase your `:range`
        # to include a negative value.
        #
        #     sortable_float_attr :position, :range => (-10..10)
        #
        # AWS::Record will add 10 to all values and zero pad them
        # (e.g. -10.0 will be represented as '00.0' and 10 will be represented as
        # '20.0').  This will allow the values to be compared lexicographically.
        #
        # @note If you change the `:range` after some values have been persisted
        #   you must also manually migrate all of the old values to have the
        #   correct padding & offset or they will be interpreted differently.
        #
        # @param [Symbol] name The name of the attribute.
        # @param [Hash] options
        # @option options [Range] :range The range of numbers this attribute
        #   should represent.  The min and max values of this range will determine
        #   how many digits of precision are required and how much of an offset
        #   is required to make the numbers sort lexicographically.
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple values.
        def sortable_float_attr name, options = {}
          add_attribute(Attributes::SortableFloatAttr.new(name, options))
        end

        # Adds a boolean attribute to this class.
        #
        # @example
        #
        #   class Book < AWS::Record::Model
        #     boolean_attr :read
        #   end
        #
        #   b = Book.new
        #   b.read? # => false
        #   b.read = true
        #   b.read? # => true
        #
        #   listing = Listing.new(:score => '123.456'
        #   listing.score # => 123.456
        #
        # @param [Symbol] name The name of the attribute.
        def boolean_attr name, options = {}

          attr = add_attribute(Attributes::BooleanAttr.new(name, options))

          # add the boolean question mark method
          define_method("#{attr.name}?") do
            !!__send__(attr.name)
          end

        end

        # Adds a datetime attribute to this class.
        #
        # @example A standard datetime attribute
        #
        #   class Recipe < AWS::Record::Model
        #     datetime_attr :invented
        #   end
        #
        #   recipe = Recipe.new(:invented => Time.now)
        #   recipe.invented #=> <DateTime ...>
        #
        # If you add a datetime_attr for `:created_at` and/or `:updated_at` those
        # will be automanaged.
        #
        # @param [Symbol] name The name of the attribute.
        #
        # @param [Hash] options
        #
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple date times.
        #
        def datetime_attr name, options = {}
          add_attribute(Record::Attributes::DateTimeAttr.new(name, options))
        end

        # Adds a date attribute to this class.
        #
        # @example A standard date attribute
        #
        #   class Person < AWS::Record::Model
        #     date_attr :birthdate
        #   end
        #
        #   baby = Person.new
        #   baby.birthdate = Time.now
        #   baby.birthdate #=> <Date: ....>
        #
        # @param [Symbol] name The name of the attribute.
        #
        # @param [Hash] options
        #
        # @option options [Boolean] :set (false) When true this attribute
        #   can have multiple dates.
        #
        def date_attr name, options = {}
          add_attribute(Record::Attributes::DateAttr.new(name, options))
        end

        # A convenience method for adding the standard two datetime attributes
        # `:created_at` and `:updated_at`.
        #
        # @example
        #
        #   class Recipe < AWS::Record::Model
        #     timestamps
        #   end
        #
        #   recipe = Recipe.new
        #   recipe.save
        #   recipe.created_at #=> <DateTime ...>
        #   recipe.updated_at #=> <DateTime ...>
        #
        def timestamps
          c = datetime_attr :created_at
          u = datetime_attr :updated_at
          [c, u]
        end

      end
    end
  end
end