module Ensurance::DateEnsure::ClassMethods
Constants
- DATE_ENSURE_FORMATS
Public Instance Methods
ensure(thing)
click to toggle source
# File lib/ensurance/date_ensure.rb, line 14 def ensure(thing) Time.zone ||= 'UTC' case thing.class.name when 'NilClass' nil when 'Integer', 'Float' Time.ensure(thing).to_date when 'Date' thing when 'String' return nil if thing.blank? if thing.to_i.to_s == thing ::Time.zone.at(thing.to_i).to_date elsif thing.to_f.to_s == thing ::Time.zone.at(thing.to_f).to_date elsif thing.index('/') # Assume US Date format result = nil DATE_ENSURE_FORMATS.each do |f| begin result = Date.strptime(thing, f) break rescue ArgumentError end end raise ArgumentError, "Bad Date: #{thing}".yellow unless result result else ::Time.zone.parse(thing).to_date end else if thing.respond_to?(:to_date) thing.to_date else raise ArgumentError, "Unknown Type for Date to ensure: #{thing.class.name}" end end end
ensure!(thing)
click to toggle source
# File lib/ensurance/date_ensure.rb, line 54 def ensure!(thing) result = self.ensure(thing) raise ArgumentError, "Cannot Date.ensure(#{thing})" unless result result end