Module: ValidatesTimeliness::Conversion
- Included in:
- Validator
- Defined in:
- lib/validates_timeliness/conversion.rb
Instance Method Summary collapse
- #dummy_time(value) ⇒ Object
- #evaluate_option_value(value, record) ⇒ Object
- #parse(value) ⇒ Object
- #restriction_shorthand?(symbol) ⇒ Boolean
- #type_cast_value(value, type) ⇒ Object
Instance Method Details
#dummy_time(value) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/validates_timeliness/conversion.rb', line 23 def dummy_time(value) time = if value.acts_like?(:time) value = value.in_time_zone if @timezone_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 @timezone_aware)) end |
#evaluate_option_value(value, record) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/validates_timeliness/conversion.rb', line 34 def evaluate_option_value(value, record) case value when Time, Date value when String parse(value) when Symbol if !record.respond_to?(value) && restriction_shorthand?(value) ValidatesTimeliness.restriction_shorthand_symbols[value].call else evaluate_option_value(record.send(value), record) end when Proc result = value.arity > 0 ? value.call(record) : value.call evaluate_option_value(result, record) else value end end |
#parse(value) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/validates_timeliness/conversion.rb', line 58 def parse(value) return nil if value.nil? if ValidatesTimeliness.use_plugin_parser Timeliness::Parser.parse(value, @type, :zone => (:current if @timezone_aware), :format => [:format], :strict => false) else @timezone_aware ? Time.zone.parse(value) : value.to_time(ValidatesTimeliness.default_timezone) end rescue ArgumentError, TypeError nil end |
#restriction_shorthand?(symbol) ⇒ Boolean
54 55 56 |
# File 'lib/validates_timeliness/conversion.rb', line 54 def restriction_shorthand?(symbol) ValidatesTimeliness.restriction_shorthand_symbols.keys.include?(symbol) end |
#type_cast_value(value, type) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/validates_timeliness/conversion.rb', line 4 def type_cast_value(value, type) return nil if value.nil? || !value.respond_to?(:to_time) value = value.in_time_zone if value.acts_like?(:time) && @timezone_aware value = case type when :time dummy_time(value) when :date value.to_date when :datetime value.is_a?(Time) ? value : value.to_time end if [:ignore_usec] && value.is_a?(Time) Timeliness::Parser.make_time(Array(value).reverse[4..9], (:current if @timezone_aware)) else value end end |