Class: ULIDAttribute
- Inherits:
-
ActiveRecord::Type::String
- Object
- ActiveRecord::Type::String
- ULIDAttribute
- Defined in:
- lib/activerecord/ulid/ulid_attribute.rb
Instance Attribute Summary collapse
-
#initial ⇒ Object
readonly
Returns the value of attribute initial.
-
#nullable ⇒ Object
readonly
Returns the value of attribute nullable.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Instance Method Summary collapse
- #cast(value) ⇒ Object
-
#initialize(precision: nil, limit: nil, scale: nil, prefix: nil, nullable: false, initial: true) ⇒ ULIDAttribute
constructor
A new instance of ULIDAttribute.
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
#initial ⇒ Object (readonly)
Returns the value of attribute initial.
4 5 6 |
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 4 def initial @initial end |
#nullable ⇒ Object (readonly)
Returns the value of attribute nullable.
4 5 6 |
# File 'lib/activerecord/ulid/ulid_attribute.rb', line 4 def nullable @nullable end |
#prefix ⇒ Object (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 |