Sha256: 17f72c5659d617802da8cd0df27cf37e12ed74d34dc3b8ca7174becaec4d338a

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 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

      def ensure!(thing)
        result = self.ensure(thing)
        raise ArgumentError, "Cannot Date.ensure(#{thing})" unless result
        result
      end
    end
  end
end

::Date.prepend(Ensurance::DateEnsure)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ensurance-0.1.11 lib/ensurance/date_ensure.rb
ensurance-0.1.10 lib/ensurance/date_ensure.rb
ensurance-0.1.9 lib/ensurance/date_ensure.rb
ensurance-0.1.8 lib/ensurance/date_ensure.rb
ensurance-0.1.7 lib/ensurance/date_ensure.rb