Sha256: 7b45dfdf2d447c6a286bdd72e4189ac9f503df513e4583a239a0b3908d1818fd

Contents?: true

Size: 1.39 KB

Versions: 5

Compression:

Stored size: 1.39 KB

Contents

# NOTE: https://blog.daveallie.com/clean-monkey-patching/

require 'active_support/core_ext/date'

module Ensurance
  module DateEnsure
    def self.prepended(base)
      base.singleton_class.prepend(ClassMethods)
    end

    module ClassMethods
      DATE_ENSURE_FORMATS = %w|%m/%d/%Y %Y/%m/%d|.freeze
      def ensure(thing)
        case thing.class.name
        when "NilClass"
          nil
        when "Integer","Float"
          Time.ensure(thing).to_date
        when "Date"
          thing
        when "String"
          if thing.to_i.to_s == thing
            ::Time.at(thing.to_i).to_date
          elsif thing.to_f.to_s == thing
            ::Time.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.new("Bad Date: #{thing}".yellow) unless result
            result
          else
            ::Date.parse(thing)
          end
        else
          if thing.respond_to?(:to_date)
            thing.to_date
          else
            raise ArgumentError.new("Unknown Type for Date to ensure: #{thing.class.name}")
          end
        end
      end
    end
  end
end

::Date.prepend(Ensurance::DateEnsure)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ensurance-0.1.6 lib/ensurance/date_ensure.rb
ensurance-0.1.5 lib/ensurance/date_ensure.rb
ensurance-0.1.4 lib/ensurance/date_ensure.rb
ensurance-0.1.3 lib/ensurance/date_ensure.rb
ensurance-0.1.2 lib/ensurance/date_ensure.rb