Class: ActiveModel::Type::Helpers::AcceptsMultiparameterTime

Inherits:
Module
  • Object
show all
Defined in:
activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb

Overview

:nodoc:

Constant Summary

Constants inherited from Module

Module::DELEGATION_RESERVED_KEYWORDS, Module::DELEGATION_RESERVED_METHOD_NAMES, Module::RUBY_RESERVED_KEYWORDS

Instance Method Summary collapse

Methods inherited from Module

#alias_attribute, #alias_method_chain, #anonymous?, #attr_internal_accessor, #attr_internal_reader, #attr_internal_writer, #delegate, #deprecate, #local_constants, #mattr_accessor, #mattr_reader, #mattr_writer, #method_visibility, #parent, #parent_name, #parents, #qualified_const_defined?, #qualified_const_get, #qualified_const_set, #reachable?, #redefine_method, #remove_possible_method, #remove_possible_singleton_method, #thread_mattr_accessor, #thread_mattr_reader, #thread_mattr_writer

Methods included from Module::Concerning

#concern, #concerning

Constructor Details

#initialize(defaults: {}) ⇒ AcceptsMultiparameterTime

Returns a new instance of AcceptsMultiparameterTime.


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'activemodel/lib/active_model/type/helpers/accepts_multiparameter_time.rb', line 5

def initialize(defaults: {})
  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_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