module AmericanDateParsing

Constants

VERSION

Public Class Methods

default_format() click to toggle source
# File lib/american_date_parsing.rb, line 10
def self.default_format
  @default_format ||= %r{\d{1,2}#{delimiter}\d{1,2}#{delimiter}\d{2,4}}
end

Public Instance Methods

parse_as_americanized_date(*attributes) click to toggle source
# File lib/american_date_parsing.rb, line 18
def parse_as_americanized_date(*attributes)
  options = attributes.extract_options!

  if !defined?(ActiveRecord::Base) || !(self < ActiveRecord::Base)
    send(:include, AmericanDateParsing::Accessors)
  end

  attributes.each do |attribute|
    attr_accessor :"_raw_#{attribute}"

    if options[:validate].present?
      validates attribute, 'american_date_parsing/american_date' => options[:validate]
    end

    define_method "#{attribute}=" do |date_string|
      parsed = if date_string.respond_to?(:strftime)
        date_string.to_date
      else
        Chronic.parse(date_string.to_s).try(:to_date)
      end

      send(:"_raw_#{attribute}=", date_string)
      write_attribute(attribute, parsed)
    end
  end
end