Class: AWS::Record::Model::Attributes::SortableIntegerAttr

Inherits:
IntegerAttr show all
Defined in:
lib/aws/record/model/attributes.rb

Instance Attribute Summary

Attributes inherited from Attributes::BaseAttr

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attributes::IntegerAttr

type_cast

Methods inherited from Attributes::BaseAttr

allow_set?, #default_value, #deserialize, #persist_as, #serialize, #set?, #type_cast

Constructor Details

#initialize(name, options = {}) ⇒ SortableIntegerAttr

Returns a new instance of SortableIntegerAttr.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
# File 'lib/aws/record/model/attributes.rb', line 42

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

Class Method Details

.deserialize(string_value, options = {}) ⇒ Object



80
81
82
83
84
# File 'lib/aws/record/model/attributes.rb', line 80

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

.serialize(integer, options = {}) ⇒ String

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'

Parameters:

  • integer (Integer)

    The number to serialize.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :range (required, 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.

Returns:

  • (String)

    A serialized representation of the integer.



71
72
73
74
75
76
77
78
# File 'lib/aws/record/model/attributes.rb', line 71

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