Class: ValidatesTimeliness::Extensions::AcceptsMultiparameterTime

Inherits:
Module
  • Object
show all
Defined in:
lib/validates_timeliness/extensions/multiparameter_handler.rb

Instance Method Summary collapse

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
32
33
34
35
# File 'lib/validates_timeliness/extensions/multiparameter_handler.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.values_at(1,2,3).all?{ |v| v.present? } &&
                    Date.valid_civil?(*values_hash.values_at(1,2,3))

    values = values_hash.sort.map(&:last)
    ::Time.send(default_timezone, *values)
  end
  private :value_from_multiparameter_assignment

end