Sha256: b059cacadd568c8d5d730324a096677b893d70df5b583084a4f9c7a8fc4110dd
Contents?: true
Size: 1.15 KB
Versions: 3
Compression:
Stored size: 1.15 KB
Contents
# frozen_string_literal: true module Normalizy module Filters module Date class << self 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 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 def format(options) options.fetch :format, '%F' end def time_zone(options) options.fetch :time_zone, 'UTC' end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
normalizy-1.7.0 | lib/normalizy/filters/date.rb |
normalizy-1.6.0 | lib/normalizy/filters/date.rb |
normalizy-1.5.0 | lib/normalizy/filters/date.rb |