Class: WeakParameters::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/weak_parameters/base_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, key, options = {}) ⇒ BaseValidator

Returns a new instance of BaseValidator.



5
6
7
8
9
# File 'lib/weak_parameters/base_validator.rb', line 5

def initialize(params, key, options = {})
  @params = params
  @key = key
  @options = options
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/weak_parameters/base_validator.rb', line 3

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/weak_parameters/base_validator.rb', line 3

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/weak_parameters/base_validator.rb', line 3

def params
  @params
end

Instance Method Details

#required?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/weak_parameters/base_validator.rb', line 15

def required?
  !!options[:required]
end

#typeObject



19
20
21
# File 'lib/weak_parameters/base_validator.rb', line 19

def type
  self.class.name.split("::").last.sub(/Validator$/, "").underscore.to_sym
end

#validateObject



11
12
13
# File 'lib/weak_parameters/base_validator.rb', line 11

def validate
  raise_error if required? && nil? || exist? && invalid_type?
end