class ActiveRecordUuid::Serializer

Attributes

type[R]

Public Class Methods

new(type) click to toggle source
# File lib/active_record_uuid/serializer.rb, line 4
def initialize(type)
  @type = type
end

Public Instance Methods

dump(value) click to toggle source
# File lib/active_record_uuid/serializer.rb, line 31
def dump(value)
  uuid = begin
    UUIDTools::UUID.parse(value)
  rescue ArgumentError, TypeError
    nil
  end

  case type
  when :binary
    uuid.raw
  when :base64
    Base64.encode64(uuid.raw).strip
  when :hexdigest
    uuid.hexdigest
  when :string
    uuid.to_s
  end
end
load(value) click to toggle source
# File lib/active_record_uuid/serializer.rb, line 8
def load(value)
  return nil if value.nil?

  begin
    uuid = case type
    when :binary
      UUIDTools::UUID.parse_raw(value)
    when :base64
      UUIDTools::UUID.parse_raw(Base64.decode64(value))
    when :hexdigest
      UUIDTools::UUID.parse_hexdigest(value)
    when :string
      UUIDTools::UUID.parse(value)
    end
    raise ArgumentError unless uuid.valid?

    uuid.to_s
  rescue ArgumentError, TypeError
    raise ActiveRecord::SerializationTypeMismatch,
      "Attribute was supposed to be a valid uuid, but was #{value}"
  end
end