Module: ParamsValidator::ValidParams

Defined in:
lib/params_validator/valid_params.rb

Overview

Module to be included in a class which methods should be validated

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
# File 'lib/params_validator/valid_params.rb', line 12

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#load_rules(rules) ⇒ Object



42
43
44
# File 'lib/params_validator/valid_params.rb', line 42

def load_rules(rules)
  self.class.load_rules(rules)
end

#previous_validation(params = nil, interface_method = nil) ⇒ Object

This method is called to validate the request parameters to a specific method before calling the method itself

Parameters:

  • params (defaults to: nil)

    Hash of parameters to validate

  • interface_method (defaults to: nil)

    String containing the fully qualified method name (Module1::Module2::Class::method)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/params_validator/valid_params.rb', line 21

def previous_validation(params = nil, interface_method = nil)
  unless validator.nil?
    if interface_method.nil?
      interface = caller[0][/^(.*)(\.rb).*$/, 1].split('/').last
      interface = caller[0][/^(.*)(\.rb).*$/, 1].split('/')
      interface.shift
      interface = interface.map!{|x| x.capitalize}.join("::")
      method    = caller[0][/`([^']*)'/, 1]
      interface_method = [interface, method].join("::")
    else
      # Got directly the method name in interface_method
    end
    params.instance_of?(Array) and params = params[0]
    validator.validate_params(interface_method, params)
  end
end

#validate_method(params = nil, &block) ⇒ Object

This method is used for implicit validations (ruleset specification inside a method)

return true if params fit the ruleset defined in &block raise ArgumentError if an error occurred

Parameters:

  • params (defaults to: nil)

    method call parameters

  • &block

    DSL that defines the validation ruleset



53
54
55
56
57
58
59
# File 'lib/params_validator/valid_params.rb', line 53

def validate_method(params = nil, &block)
  method = MethodValidation.new("foo_bar")
  
  block_given? and method.block &block
  
  Validator.validate_ruleset(method.parameters, params)
end

#validatorObject



38
39
40
# File 'lib/params_validator/valid_params.rb', line 38

def validator
  self.class.validator
end