Sha256: 99ff7b95c2151279a3bd17194169cd7770f14d6aa3701218222f76c9387ccc9c

Contents?: true

Size: 1.22 KB

Versions: 63

Compression:

Stored size: 1.22 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 => 2000, 2 => 1, 3 => 1, 4 => 0, 5 => 0 }
      )

      def type
        :time
      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

63 entries across 63 versions & 6 rubygems

Version Path
activemodel-6.1.7.10 lib/active_model/type/time.rb
activemodel-6.1.7.9 lib/active_model/type/time.rb
activemodel-6.1.7.8 lib/active_model/type/time.rb
activemodel-6.1.7.7 lib/active_model/type/time.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/type/time.rb
activemodel-6.1.7.6 lib/active_model/type/time.rb
activemodel-6.1.7.5 lib/active_model/type/time.rb
activemodel-6.1.7.4 lib/active_model/type/time.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activemodel-6.1.6.1/lib/active_model/type/time.rb
activemodel-6.1.7.3 lib/active_model/type/time.rb
activemodel-6.1.7.2 lib/active_model/type/time.rb
activemodel-6.1.7.1 lib/active_model/type/time.rb
activemodel-6.0.6.1 lib/active_model/type/time.rb
activemodel-6.1.7 lib/active_model/type/time.rb
activemodel-6.0.6 lib/active_model/type/time.rb
activemodel-6.1.6.1 lib/active_model/type/time.rb
activemodel-6.0.5.1 lib/active_model/type/time.rb
activemodel-6.0.5 lib/active_model/type/time.rb
activemodel-6.1.6 lib/active_model/type/time.rb
activemodel-6.1.5.1 lib/active_model/type/time.rb