Class: ActsAsHavingStringId::StringId

Inherits:
ActiveRecord::Type::Value
  • Object
show all
Defined in:
lib/acts_as_having_string_id/string_id.rb

Instance Method Summary collapse

Constructor Details

#initialize(tea) ⇒ StringId

Returns a new instance of StringId.



3
4
5
# File 'lib/acts_as_having_string_id/string_id.rb', line 3

def initialize(tea)
  @tea = tea
end

Instance Method Details

#serialize(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/acts_as_having_string_id/string_id.rb', line 7

def serialize(value)
  if value.is_a? String
    i = @tea.decrypt(value.base62_decode)
    if i >= 2**31
      # Since Postgres SERIAL is a signed 32-bit integer, we can
      # only represent integers up until (2**32)-1. If we're
      # serializing a larger id, we want a not found rather than
      # a postgres datatype out of bounds error. WHERE id = -1
      # will definitely not be found.
      return -1
    end
    return i
  else
    value
  end
end