Class: ActiveType::TypeCaster
- Inherits:
-
Object
- Object
- ActiveType::TypeCaster
- Defined in:
- lib/active_type/type_caster.rb
Defined Under Namespace
Modules: NativeCasters
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(type, native_caster) ⇒ TypeCaster
constructor
A new instance of TypeCaster.
- #native_type_cast_from_user(value) ⇒ Object
- #type_cast_from_user(value) ⇒ Object
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 |