Class: Restapi::Validator::ArrayValidator
Overview
arguments value must be one of given in array
Instance Attribute Summary
#param_name
Class Method Summary
collapse
Instance Method Summary
collapse
find, #to_json, #to_s, #valid?
Constructor Details
Returns a new instance of ArrayValidator.
103
104
105
|
# File 'lib/restapi/validator.rb', line 103
def initialize(argument)
@array = argument
end
|
Class Method Details
.build(argument, options, proc) ⇒ Object
121
122
123
|
# File 'lib/restapi/validator.rb', line 121
def self.build(argument, options, proc)
self.new(argument) if argument.is_a?(Array)
end
|
Instance Method Details
#description ⇒ Object
129
130
131
|
# File 'lib/restapi/validator.rb', line 129
def description
"Parameter has to be one of: #{@array.join(', ')}."
end
|
#error ⇒ Object
125
126
127
|
# File 'lib/restapi/validator.rb', line 125
def error
"Parameter #{@param_name} has bad value (#{@error_value.inspect}). Expecting one of: #{@array.join(',')}."
end
|
#validate(value) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/restapi/validator.rb', line 107
def validate(value)
@array.find do |expected|
expected_class = expected.class
expected_class = Integer if expected_class == Fixnum
begin
converted_value = Kernel.send(expected_class.to_s, value)
rescue ArgumentError
false
end
converted_value === expected
end
end
|