Sha256: 96a9cb354106751123edc67da9b4a26791ab048adac236eda1f7ade04b558197

Contents?: true

Size: 1.25 KB

Versions: 38

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module ActiveModel
  module Type
    module Helpers # :nodoc: all
      class AcceptsMultiparameterTime < Module
        def initialize(defaults: {})
          define_method(:serialize) do |value|
            super(cast(value))
          end

          define_method(:cast) do |value|
            if value.is_a?(Hash)
              value_from_multiparameter_assignment(value)
            else
              super(value)
            end
          end

          define_method(:assert_valid_value) do |value|
            if value.is_a?(Hash)
              value_from_multiparameter_assignment(value)
            else
              super(value)
            end
          end

          define_method(:value_constructed_by_mass_assignment?) do |value|
            value.is_a?(Hash)
          end

          define_method(:value_from_multiparameter_assignment) do |values_hash|
            defaults.each do |k, v|
              values_hash[k] ||= v
            end
            return unless values_hash[1] && values_hash[2] && values_hash[3]
            values = values_hash.sort.map(&:last)
            ::Time.send(default_timezone, *values)
          end
          private :value_from_multiparameter_assignment
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 4 rubygems

Version Path
activemodel-6.0.6.1 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.6 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.5.1 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.5 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.8 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.7 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.6 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.5 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.4 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.3 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.2 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4.1 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.4 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.7 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.6 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.5 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.4 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.3 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.2 lib/active_model/type/helpers/accepts_multiparameter_time.rb
activemodel-6.0.3.1 lib/active_model/type/helpers/accepts_multiparameter_time.rb