Class: ULIDAttribute

Inherits:
ActiveRecord::Type::String
  • Object
show all
Defined in:
lib/activerecord/ulid/ulid_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(precision: nil, limit: nil, scale: nil, prefix: nil, nullable: false, initial: true) ⇒ ULIDAttribute



8
9
10
11
12
13
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 8

def initialize(precision: nil, limit: nil, scale: nil, prefix: nil, nullable: false, initial: true)
  @prefix = prefix
  @initial = initial
  @nullable = nullable
  super()
end

Instance Attribute Details

#initialObject (readonly)

Returns the value of attribute initial.



4
5
6
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 4

def initial
  @initial
end

#nullableObject (readonly)

Returns the value of attribute nullable.



4
5
6
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 4

def nullable
  @nullable
end

#prefixObject (readonly)

Returns the value of attribute prefix.



4
5
6
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 4

def prefix
  @prefix
end

Instance Method Details

#cast(value) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 15

def cast(value)
  return value if value.present?
  return if value.nil? && nullable

  value = ULID.generate if value.nil? && initial
  return value if value.empty?

  [prefix, value].compact.join
end