Class: ParamsValidator::MethodValidation

Inherits:
Object
  • Object
show all
Defined in:
lib/params_validator/method_validation.rb

Overview

This class models the information required to validate a specific method call

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, &block) ⇒ MethodValidation

MethodValidation constructor

Parameters:

  • method_name

    fully qualified method name (Module::Class::method)

  • block

    the block to be executed



25
26
27
28
29
# File 'lib/params_validator/method_validation.rb', line 25

def initialize(method_name, &block)
  @method_name = method_name
  @parameters = []
  block_given? and self.instance_exec &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

DSL that defines the validation rules. parameter (Object, :optional = false) { block }



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/params_validator/method_validation.rb', line 51

def method_missing(meth, *args, &block)

  if args.length < 2
    optional = false
  else
    if args[1].eql?(:optional)
      optional = true
    else
      optional = false
    end
  end

  parameter = Parameter.new(meth, args.length < 1 ? Object : args[0], optional, block)
  @parameters.push parameter
end

Instance Attribute Details

#method_nameObject

fully qualified method name



15
16
17
# File 'lib/params_validator/method_validation.rb', line 15

def method_name
  @method_name
end

#parametersObject

parameters expected by the method



18
19
20
# File 'lib/params_validator/method_validation.rb', line 18

def parameters
  @parameters
end

Instance Method Details

#block(&block) ⇒ Object

Execute validation block in this object scope



43
44
45
# File 'lib/params_validator/method_validation.rb', line 43

def block(&block)
  block_given? and self.instance_exec &block
end

#mandatoriesObject

get mandatory parameters



32
33
34
35
36
37
38
# File 'lib/params_validator/method_validation.rb', line 32

def mandatories
  values = []
  @parameters.each{|param|
    param.mandatory? and values << param
  }
  values
end

#to_sObject



67
68
69
# File 'lib/params_validator/method_validation.rb', line 67

def to_s
  "method <#{self.method_name}> => #{parameters.to_s}"
end