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(controller, key, options = {}, &block) ⇒ BaseValidator

Returns a new instance of BaseValidator.



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

def initialize(controller, key, options = {}, &block)
  @controller = controller
  @key = key
  @options = options
  @block = block
  @path = []
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
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

Instance Method Details

#keyObject



30
31
32
# File 'lib/weak_parameters/base_validator.rb', line 30

def key
  path[-1]
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  !!options[:required]
end

#strong?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/weak_parameters/base_validator.rb', line 22

def strong?
  !!options[:strong]
end

#strong_params(*path) ⇒ Object



34
35
36
37
# File 'lib/weak_parameters/base_validator.rb', line 34

def strong_params(*path)
  @path = path
  exist? && strong? ? {key => value} : {}
end

#typeObject



26
27
28
# File 'lib/weak_parameters/base_validator.rb', line 26

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

#validate(*path) ⇒ Object



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

def validate(*path)
  @path = path
  handle_failure unless valid?
end