Class: Trax::Model::Attributes::Types::String::TypeCaster

Inherits:
ActiveRecord::Type::String
  • Object
show all
Defined in:
lib/trax/model/attributes/types/string.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, target_klass:) ⇒ TypeCaster

Returns a new instance of TypeCaster.



25
26
27
28
29
# File 'lib/trax/model/attributes/types/string.rb', line 25

def initialize(*args, target_klass:)
  super(*args)

  @target_klass = target_klass
end

Instance Method Details

#type_cast_for_database(value) ⇒ Object



39
40
41
# File 'lib/trax/model/attributes/types/string.rb', line 39

def type_cast_for_database(value)
  value.try(:to_s)
end

#type_cast_from_database(value) ⇒ Object



35
36
37
# File 'lib/trax/model/attributes/types/string.rb', line 35

def type_cast_from_database(value)
  value.present? ? @target_klass.new(value) : value
end

#type_cast_from_user(value) ⇒ Object



31
32
33
# File 'lib/trax/model/attributes/types/string.rb', line 31

def type_cast_from_user(value)
  value.is_a?(@target_klass) ? @target_klass : @target_klass.new(value)
end