Class: Restapi::Validator::ArrayValidator

Inherits:
BaseValidator show all
Defined in:
lib/restapi/validator.rb

Overview

arguments value must be one of given in array

Instance Attribute Summary

Attributes inherited from BaseValidator

#param_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseValidator

find, #to_json, #to_s, #valid?

Constructor Details

#initialize(argument) ⇒ ArrayValidator

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

#descriptionObject



129
130
131
# File 'lib/restapi/validator.rb', line 129

def description
  "Parameter has to be one of: #{@array.join(', ')}."
end

#errorObject



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