Class: DataMapper::Validate::AcceptanceValidator
- Inherits:
-
GenericValidator
show all
- Defined in:
- lib/gems/dm-validations-0.9.9/lib/dm-validations/acceptance_validator.rb
Overview
Instance Attribute Summary
#if_clause, #unless_clause
Class Method Summary
collapse
Instance Method Summary
collapse
#==, #add_error, #execute?, #field_name
Constructor Details
#initialize(field_name, options = {}) ⇒ AcceptanceValidator
Returns a new instance of AcceptanceValidator.
14
15
16
17
18
19
20
21
|
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/acceptance_validator.rb', line 14
def initialize(field_name, options = {})
super
@options = options
@field_name = field_name
@options[:allow_nil] = true unless @options.include?(:allow_nil)
@options[:accept] ||= ["1",1,"true",true,"t"]
@options[:accept] = Array(@options[:accept])
end
|
Class Method Details
.default_message_for_field(field_name) ⇒ Object
10
11
12
|
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/acceptance_validator.rb', line 10
def self.default_message_for_field(field_name)
'%s is not accepted'.t(Extlib::Inflection.humanize(field_name))
end
|
Instance Method Details
#call(target) ⇒ Object
23
24
25
26
27
28
29
30
31
|
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/acceptance_validator.rb', line 23
def call(target)
unless valid?(target)
error_message = @options[:message] || DataMapper::Validate::AcceptanceValidator.default_message_for_field(@field_name)
add_error(target, error_message , @field_name)
return false
end
return true
end
|
#valid?(target) ⇒ Boolean
33
34
35
36
37
38
39
|
# File 'lib/gems/dm-validations-0.9.9/lib/dm-validations/acceptance_validator.rb', line 33
def valid?(target)
field_value = target.instance_variable_get("@#{@field_name}")
return true if @options[:allow_nil] && field_value.nil?
return false if !@options[:allow_nil] && field_value.nil?
@options[:accept].include?(field_value)
end
|