Sha256: 4357e1f80c58b9f2dc8302a85b97803bd8ea79f359b7a8583057f9ba62474e0f

Contents?: true

Size: 1.28 KB

Versions: 23

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    class Time < Value # :nodoc:
      include Helpers::Timezone
      include Helpers::TimeValue
      include Helpers::AcceptsMultiparameterTime.new(
        defaults: { 1 => 1970, 2 => 1, 3 => 1, 4 => 0, 5 => 0 }
      )

      def type
        :time
      end

      def serialize(value)
        super(cast(value))
      end

      def user_input_in_time_zone(value)
        return unless value.present?

        case value
        when ::String
          value = "2000-01-01 #{value}"
          time_hash = ::Date._parse(value)
          return if time_hash[:hour].nil?
        when ::Time
          value = value.change(year: 2000, day: 1, month: 1)
        end

        super(value)
      end

      private

        def cast_value(value)
          return apply_seconds_precision(value) unless value.is_a?(::String)
          return if value.empty?

          dummy_time_value = value.sub(/\A(\d\d\d\d-\d\d-\d\d |)/, "2000-01-01 ")

          fast_string_to_time(dummy_time_value) || begin
            time_hash = ::Date._parse(dummy_time_value)
            return if time_hash[:hour].nil?
            new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset))
          end
        end
    end
  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
activemodel-5.2.8.1 lib/active_model/type/time.rb
activemodel-5.2.8 lib/active_model/type/time.rb
activemodel-5.2.7.1 lib/active_model/type/time.rb
activemodel-5.2.7 lib/active_model/type/time.rb
activemodel-5.2.6.3 lib/active_model/type/time.rb
activemodel-5.2.6.2 lib/active_model/type/time.rb
activemodel-5.2.6.1 lib/active_model/type/time.rb
activemodel-5.2.6 lib/active_model/type/time.rb
activemodel-5.2.4.6 lib/active_model/type/time.rb
activemodel-5.2.5 lib/active_model/type/time.rb
activemodel-5.2.4.5 lib/active_model/type/time.rb
activemodel-5.2.4.4 lib/active_model/type/time.rb
activemodel-5.2.4.3 lib/active_model/type/time.rb
activemodel-5.2.4.2 lib/active_model/type/time.rb
activemodel-5.2.4.1 lib/active_model/type/time.rb
activemodel-5.2.4 lib/active_model/type/time.rb
activemodel-5.2.4.rc1 lib/active_model/type/time.rb
spiral_form-0.1.1 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/time.rb
spiral_form-0.1.0 vendor/bundle/gems/activemodel-5.2.3/lib/active_model/type/time.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/activemodel-5.2.3/lib/active_model/type/time.rb