Class: ValidatesTimeliness::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_timeliness/converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, format: nil, ignore_usec: false, time_zone_aware: false) ⇒ Converter

Returns a new instance of Converter.



5
6
7
8
9
10
# File 'lib/validates_timeliness/converter.rb', line 5

def initialize(type:, format: nil, ignore_usec: false, time_zone_aware: false)
  @type = type
  @format = format
  @ignore_usec = ignore_usec
  @time_zone_aware = time_zone_aware
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



3
4
5
# File 'lib/validates_timeliness/converter.rb', line 3

def format
  @format
end

#ignore_usecObject (readonly)

Returns the value of attribute ignore_usec.



3
4
5
# File 'lib/validates_timeliness/converter.rb', line 3

def ignore_usec
  @ignore_usec
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/validates_timeliness/converter.rb', line 3

def type
  @type
end

Instance Method Details

#dummy_time(value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/validates_timeliness/converter.rb', line 33

def dummy_time(value)
  time = if value.acts_like?(:time)
    value = value.in_time_zone if time_zone_aware?
    [value.hour, value.min, value.sec]
  else
    [0,0,0]
  end
  values = ValidatesTimeliness.dummy_date_for_time_type + time
  Timeliness::Parser.make_time(values, (:current if time_zone_aware?))
end

#evaluate(value, scope = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/validates_timeliness/converter.rb', line 44

def evaluate(value, scope=nil)
  case value
  when Time, Date
    value
  when String
    parse(value)
  when Symbol
    if !scope.respond_to?(value) && restriction_shorthand?(value)
      ValidatesTimeliness.restriction_shorthand_symbols[value].call
    else
      evaluate(scope.send(value))
    end
  when Proc
    result = value.arity > 0 ? value.call(scope) : value.call
    evaluate(result, scope)
  else
    value
  end
end

#parse(value) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/validates_timeliness/converter.rb', line 68

def parse(value)
  return nil if value.nil?

  if ValidatesTimeliness.use_plugin_parser
    Timeliness::Parser.parse(value, type, zone: (:current if time_zone_aware?), format: format, strict: false)
  else
    time_zone_aware? ? Time.zone.parse(value) : value.to_time(ValidatesTimeliness.default_timezone)
  end
rescue ArgumentError, TypeError
  nil
end

#restriction_shorthand?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/validates_timeliness/converter.rb', line 64

def restriction_shorthand?(symbol)
  ValidatesTimeliness.restriction_shorthand_symbols.keys.include?(symbol)
end

#time_zone_aware?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/validates_timeliness/converter.rb', line 80

def time_zone_aware?
  @time_zone_aware
end

#type_cast_value(value) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/validates_timeliness/converter.rb', line 12

def type_cast_value(value)
  return nil if value.nil? || !value.respond_to?(:to_time)

  value = value.in_time_zone if value.acts_like?(:time) && time_zone_aware?
  value = case type
  when :time
    dummy_time(value)
  when :date
    value.to_date
  when :datetime
    value.is_a?(Time) ? value : value.to_time
  else
    value
  end
  if ignore_usec && value.is_a?(Time)
    Timeliness::Parser.make_time(Array(value).reverse[4..9], (:current if time_zone_aware?))
  else
    value
  end
end