Class: ValidatesTimeliness::Validator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidatesTimeliness::Validator
- Defined in:
- lib/validates_timeliness/validator.rb
Constant Summary collapse
- RESTRICTIONS =
{ :is_at => :==, :before => :<, :after => :>, :on_or_before => :<=, :on_or_after => :>=, }.freeze
- DEFAULT_ERROR_VALUE_FORMATS =
{ :date => '%Y-%m-%d', :time => '%H:%M:%S', :datetime => '%Y-%m-%d %H:%M:%S' }.freeze
- RESTRICTION_ERROR_MESSAGE =
"Error occurred validating %s for %s restriction:\n%s"
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#converter ⇒ Object
readonly
Returns the value of attribute converter.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #add_error(record, attr_name, message, value = nil) ⇒ Object
- #attribute_raw_value(record, attr_name) ⇒ Object
- #format_error_value(value) ⇒ Object
-
#initialize(options) ⇒ Validator
constructor
A new instance of Validator.
- #initialize_converter(record, attr_name) ⇒ Object
- #setup_timeliness_validated_attributes(model) ⇒ Object
- #time_zone_aware?(record, attr_name) ⇒ Boolean
- #validate_each(record, attr_name, value) ⇒ Object
- #validate_restrictions(record, attr_name, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ Validator
Returns a new instance of Validator.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/validates_timeliness/validator.rb', line 28 def initialize() @type = .delete(:type) || :datetime @allow_nil, @allow_blank = .delete(:allow_nil), .delete(:allow_blank) if range = .delete(:between) raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array) [:on_or_after] = range.first if range.is_a?(Range) && range.exclude_end? [:before] = range.last else [:on_or_before] = range.last end end @restrictions_to_check = RESTRICTIONS.keys & .keys super setup_timeliness_validated_attributes([:class]) if [:class] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/validates_timeliness/validator.rb', line 6 def attributes @attributes end |
#converter ⇒ Object (readonly)
Returns the value of attribute converter.
6 7 8 |
# File 'lib/validates_timeliness/validator.rb', line 6 def converter @converter end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/validates_timeliness/validator.rb', line 6 def type @type end |
Class Method Details
.kind ⇒ Object
24 25 26 |
# File 'lib/validates_timeliness/validator.rb', line 24 def self.kind :timeliness end |
Instance Method Details
#add_error(record, attr_name, message, value = nil) ⇒ Object
86 87 88 89 90 |
# File 'lib/validates_timeliness/validator.rb', line 86 def add_error(record, attr_name, , value=nil) value = format_error_value(value) if value = { message: .fetch(:"#{}_message", [:message]), restriction: value } record.errors.add(attr_name, , **) end |
#attribute_raw_value(record, attr_name) ⇒ Object
97 98 99 100 |
# File 'lib/validates_timeliness/validator.rb', line 97 def attribute_raw_value(record, attr_name) record.respond_to?(:read_timeliness_attribute_before_type_cast) && record.read_timeliness_attribute_before_type_cast(attr_name.to_s) end |
#format_error_value(value) ⇒ Object
92 93 94 95 |
# File 'lib/validates_timeliness/validator.rb', line 92 def format_error_value(value) format = I18n.t(@type, default: DEFAULT_ERROR_VALUE_FORMATS[@type], scope: 'validates_timeliness.error_value_formats') value.strftime(format) end |
#initialize_converter(record, attr_name) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/validates_timeliness/validator.rb', line 107 def initialize_converter(record, attr_name) ValidatesTimeliness::Converter.new( type: @type, time_zone_aware: time_zone_aware?(record, attr_name), format: [:format], ignore_usec: [:ignore_usec] ) end |
#setup_timeliness_validated_attributes(model) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/validates_timeliness/validator.rb', line 49 def setup_timeliness_validated_attributes(model) if model.respond_to?(:timeliness_validated_attributes) model.timeliness_validated_attributes ||= [] model.timeliness_validated_attributes |= attributes end end |
#time_zone_aware?(record, attr_name) ⇒ Boolean
102 103 104 105 |
# File 'lib/validates_timeliness/validator.rb', line 102 def time_zone_aware?(record, attr_name) record.class.respond_to?(:skip_time_zone_conversion_for_attributes) && !record.class.skip_time_zone_conversion_for_attributes.include?(attr_name.to_sym) end |
#validate_each(record, attr_name, value) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/validates_timeliness/validator.rb', line 56 def validate_each(record, attr_name, value) raw_value = attribute_raw_value(record, attr_name) || value return if (@allow_nil && raw_value.nil?) || (@allow_blank && raw_value.blank?) @converter = initialize_converter(record, attr_name) value = @converter.parse(raw_value) if value.is_a?(String) || [:format] value = @converter.type_cast_value(value) add_error(record, attr_name, :"invalid_#{@type}") and return if value.blank? validate_restrictions(record, attr_name, value) end |
#validate_restrictions(record, attr_name, value) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/validates_timeliness/validator.rb', line 70 def validate_restrictions(record, attr_name, value) @restrictions_to_check.each do |restriction| begin restriction_value = @converter.type_cast_value(@converter.evaluate([restriction], record)) unless value.send(RESTRICTIONS[restriction], restriction_value) add_error(record, attr_name, restriction, restriction_value) and break end rescue => e unless ValidatesTimeliness.ignore_restriction_errors = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e. ] add_error(record, attr_name, ) and break end end end end |