Class: ActiveType::TypeCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/active_type/type_caster.rb

Defined Under Namespace

Modules: NativeCasters

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, native_caster) ⇒ TypeCaster

Returns a new instance of TypeCaster.



15
16
17
18
# File 'lib/active_type/type_caster.rb', line 15

def initialize(type, native_caster)
  @type = type
  @native_caster = native_caster
end

Class Method Details

.get(type) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/active_type/type_caster.rb', line 4

def self.get(type)
  native_caster = if ActiveRecord::VERSION::STRING < '4.2'
    NativeCasters::DelegateToColumn.new(type)
  elsif ActiveRecord::VERSION::STRING < '5'
    NativeCasters::DelegateToRails4Type.new(type)
  else
    NativeCasters::DelegateToRails5Type.new(type)
  end
  new(type, native_caster)
end

Instance Method Details

#native_type_cast_from_user(value) ⇒ Object



33
34
35
# File 'lib/active_type/type_caster.rb', line 33

def native_type_cast_from_user(value)
  @native_caster.type_cast_from_user(value)
end

#type_cast_from_user(value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_type/type_caster.rb', line 20

def type_cast_from_user(value)
  # For some reason, Rails defines additional type casting logic
  # outside the classes that have that responsibility.
  case @type
  when :integer
    cast_integer(value)
  when :timestamp, :datetime
    cast_time(value)
  else
    native_type_cast_from_user(value)
  end
end