module Normalizy::Filters::Date

Public Class Methods

call(input, options = {}) click to toggle source
# File lib/normalizy/filters/date.rb, line 7
def call(input, options = {})
  if input.is_a?(String)
    return input if input.blank?

    Time.use_zone(time_zone(options)) do
      input = Time.zone.strptime(input, format(options))
    end
  end

  input = input.beginning_of_day if options[:adjust] == :begin && input.respond_to?(:beginning_of_day)
  input = input.end_of_day       if options[:adjust] == :end   && input.respond_to?(:end_of_day)

  input
rescue ArgumentError
  options[:object].errors.add options[:attribute], error_message(input, options)
end

Private Class Methods

error_message(input, options) click to toggle source
# File lib/normalizy/filters/date.rb, line 26
def error_message(input, options)
  I18n.t options[:attribute], scope: [
    'normalizy.errors.date', options[:object].class.name.underscore
  ], value: input, default: '%{value} is an invalid date.'
end
format(options) click to toggle source
# File lib/normalizy/filters/date.rb, line 32
def format(options)
  options.fetch :format, '%F'
end
time_zone(options) click to toggle source
# File lib/normalizy/filters/date.rb, line 36
def time_zone(options)
  options.fetch :time_zone, 'UTC'
end