Sha256: fdc63e36a4383cc9af49158cf32e74045b0dd8e99e178091541ce89385db3643

Contents?: true

Size: 1.22 KB

Versions: 16

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

16 entries across 16 versions & 4 rubygems

Version Path
activemodel-6.0.2.2 lib/active_model/type/time.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activemodel-6.0.2.1/lib/active_model/type/time.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activemodel-6.0.2.1/lib/active_model/type/time.rb
activemodel-6.0.2.1 lib/active_model/type/time.rb
activemodel-6.0.2 lib/active_model/type/time.rb
activemodel-6.0.2.rc2 lib/active_model/type/time.rb
activemodel-6.0.2.rc1 lib/active_model/type/time.rb
activemodel-6.0.1 lib/active_model/type/time.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activemodel-6.0.0/lib/active_model/type/time.rb
activemodel-6.0.1.rc1 lib/active_model/type/time.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activemodel-6.0.0/lib/active_model/type/time.rb
activemodel-6.0.0 lib/active_model/type/time.rb
activemodel-6.0.0.rc2 lib/active_model/type/time.rb
activemodel-6.0.0.rc1 lib/active_model/type/time.rb
activemodel-6.0.0.beta3 lib/active_model/type/time.rb
activemodel-6.0.0.beta2 lib/active_model/type/time.rb