Class: ULID::Rails::Type

Inherits:
ActiveModel::Type::Binary
  • Object
show all
Defined in:
lib/ulid/rails/type.rb

Direct Known Subclasses

PostgresqlType, SqliteType

Defined Under Namespace

Classes: Data

Instance Method Summary collapse

Instance Method Details

#assert_valid_value(value) ⇒ Object

Raises:



8
9
10
# File 'lib/ulid/rails/type.rb', line 8

def assert_valid_value(value)
  raise ArgumentError, "`#{value}` is not a ULID format" unless Data.valid_ulid?(value)
end

#cast(value) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/ulid/rails/type.rb', line 12

def cast(value)
  return nil if value.nil?

  str = value.is_a?(Data) ? value.value : value

  cast_string_to_ulid(str).value
end

#deserialize(value) ⇒ Object



27
28
29
30
31
# File 'lib/ulid/rails/type.rb', line 27

def deserialize(value)
  return nil if value.nil?

  super
end

#serialize(value) ⇒ Object



20
21
22
23
24
25
# File 'lib/ulid/rails/type.rb', line 20

def serialize(value)
  return value if value.is_a?(Data)
  return nil unless value.is_a?(String)

  cast_string_to_ulid(value)
end