Class: ActiveType::VirtualAttributes::VirtualColumn

Inherits:
ActiveRecord::ConnectionAdapters::Column
  • Object
show all
Defined in:
lib/active_type/virtual_attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, type) ⇒ VirtualColumn

Returns a new instance of VirtualColumn.



10
11
12
13
# File 'lib/active_type/virtual_attributes.rb', line 10

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

Instance Method Details

#type_cast(value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_type/virtual_attributes.rb', line 15

def type_cast(value)
  case @type
  when :integer
    case value
    when FalseClass
      0
    when TrueClass
      1
    when "", nil
      nil
    else
      value.to_i
    end
  when :timestamp, :datetime
    if ActiveRecord::Base.time_zone_aware_attributes
      time = super
      if time
        ActiveSupport::TimeWithZone.new(nil, Time.zone, time)
      else
        time
      end
    else
      super
    end
  when nil
    value
  else
    super
  end
end