123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveModel::Type::Value
Defined in: activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb

Class Method Summary

::ActiveModel::Type::Value - Inherited

.new

Initializes a type with three basic configuration settings: precision, limit, and scale.

Instance Attribute Summary

::ActiveModel::Type::Value - Inherited

#limit, #precision, #scale,
#binary?

These predicates are not documented, as I need to look further into their use, and see if they can be removed entirely.

#mutable?, #serialized?

Instance Method Summary

::ActiveModel::Type::Value - Inherited

#==, #as_json, #assert_valid_value,
#cast

::ActiveRecord::Type casts a value from user input (e.g. from a setter).

#changed?

Determines whether a value has changed for dirty checking.

#changed_in_place?

Determines whether the mutable value has been modified since it was read.

#deserialize

Converts a value from database input to the appropriate ruby type.

#eql?
#hash,
#serializable?

Returns true if this type can convert value to a type that is usable by the database.

#serialize

Casts a value from the ruby type to a type that the database knows how to understand.

#type

Returns the unique type name as a ::Symbol.

#cast_value

Convenience method for types which do not need separate type casting behavior for user and database inputs.

#force_equality?, #map,
#type_cast_for_schema

::ActiveRecord::Type casts a value for schema dumping.

#value_constructed_by_mass_assignment?

::ActiveModel::Type::SerializeCastValue - Included

Constructor Details

.new(subtype) ⇒ TimeZoneConverter

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 9

def self.new(subtype)
  self === subtype ? subtype : super
end

Instance Method Details

#cast(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 17

def cast(value)
  return if value.nil?

  if value.is_a?(Hash)
    set_time_zone_without_conversion(super)
  elsif value.respond_to?(:in_time_zone)
    begin
      super(user_input_in_time_zone(value)) || super
    rescue ArgumentError
      nil
    end
  elsif value.respond_to?(:infinite?) && value.infinite?
    value
  else
    map_avoiding_infinite_recursion(super) { |v| cast(v) }
  end
end

#convert_time_to_time_zone(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 36

def convert_time_to_time_zone(value)
  return if value.nil?

  if value.acts_like?(:time)
    value.in_time_zone
  elsif value.respond_to?(:infinite?) && value.infinite?
    value
  else
    map_avoiding_infinite_recursion(value) { |v| convert_time_to_time_zone(v) }
  end
end

#deserialize(value)

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 13

def deserialize(value)
  convert_time_to_time_zone(super)
end

#map_avoiding_infinite_recursion(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 52

def map_avoiding_infinite_recursion(value)
  map(value) do |v|
    if value.equal?(v)
      nil
    else
      yield(v)
    end
  end
end

#set_time_zone_without_conversion(value) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb', line 48

def set_time_zone_without_conversion(value)
  ::Time.zone.local_to_utc(value).try(:in_time_zone) if value
end