Class: ValidatesTimeliness::Validator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ValidatesTimeliness::Validator
show all
- Includes:
- Conversion
- 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
Instance Method Summary
collapse
Methods included from Conversion
#dummy_time, #evaluate_option_value, #parse, #restriction_shorthand?, #type_cast_value
Constructor Details
#initialize(options) ⇒ Validator
Returns a new instance of Validator.
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/validates_timeliness/validator.rb', line 25
def initialize(options)
@type = options.delete(:type) || :datetime
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
if range = options.delete(:between)
raise ArgumentError, ":between must be a Range or an Array" unless range.is_a?(Range) || range.is_a?(Array)
options[:on_or_after], options[:on_or_before] = range.first, range.last
end
@restrictions_to_check = RESTRICTIONS.keys & options.keys
super
end
|
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
7
8
9
|
# File 'lib/validates_timeliness/validator.rb', line 7
def type
@type
end
|
Instance Method Details
#add_error(record, attr_name, message, value = nil) ⇒ Object
67
68
69
70
71
|
# File 'lib/validates_timeliness/validator.rb', line 67
def add_error(record, attr_name, message, value=nil)
value = format_error_value(value) if value
message_options = { :message => options[:"#{message}_message"], :restriction => value }
record.errors.add(attr_name, message, message_options)
end
|
#attribute_raw_value(record, attr_name) ⇒ Object
78
79
80
81
|
# File 'lib/validates_timeliness/validator.rb', line 78
def attribute_raw_value(record, attr_name)
record.respond_to?(:_timeliness_raw_value_for) &&
record._timeliness_raw_value_for(attr_name)
end
|
73
74
75
76
|
# File 'lib/validates_timeliness/validator.rb', line 73
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
|
#timezone_aware?(record, attr_name) ⇒ Boolean
83
84
85
86
|
# File 'lib/validates_timeliness/validator.rb', line 83
def timezone_aware?(record, attr_name)
record.class.respond_to?(:timeliness_attribute_timezone_aware?) &&
record.class.timeliness_attribute_timezone_aware?(attr_name)
end
|
#validate_each(record, attr_name, value) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/validates_timeliness/validator.rb', line 38
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?)
@timezone_aware = timezone_aware?(record, attr_name)
value = parse(raw_value) if value.is_a?(String) || options[:format]
value = type_cast_value(value, @type)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/validates_timeliness/validator.rb', line 51
def validate_restrictions(record, attr_name, value)
@restrictions_to_check.each do |restriction|
begin
restriction_value = type_cast_value(evaluate_option_value(options[restriction], record), @type)
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
message = RESTRICTION_ERROR_MESSAGE % [ attr_name, restriction.inspect, e.message ]
add_error(record, attr_name, message) and break
end
end
end
end
|