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.



13
14
15
16
# File 'lib/active_type/type_caster.rb', line 13

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

Class Method Details

.get(type, connection) ⇒ Object



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

def self.get(type, connection)
  native_caster = if ActiveRecord::VERSION::STRING < '4.2'
    NativeCasters::DelegateToColumn.new(type)
  else
    NativeCasters::DelegateToType.new(type, connection)
  end
  new(type, native_caster)
end

Instance Method Details

#native_type_cast_from_user(value) ⇒ Object



39
40
41
# File 'lib/active_type/type_caster.rb', line 39

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

#type_cast_from_user(value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_type/type_caster.rb', line 18

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
    if value == ''
      nil
    else
      native_type_cast_from_user(value)
    end
  when :timestamp, :datetime
    time = native_type_cast_from_user(value)
    if time && ActiveRecord::Base.time_zone_aware_attributes
      time = ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
    end
    time
  else
    native_type_cast_from_user(value)
  end
end