Class: ParamValidator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/param_validator/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base



9
10
11
# File 'lib/param_validator/base.rb', line 9

def initialize( params )
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/param_validator/base.rb', line 7

def params
  @params
end

Class Method Details

.inherited(base) ⇒ Object



13
14
15
16
# File 'lib/param_validator/base.rb', line 13

def self.inherited( base )
  self.specification = {}
  super
end

.validate(param, spec) ⇒ Object



39
40
41
# File 'lib/param_validator/base.rb', line 39

def self.validate( param, spec )
  self.specification = specification.merge( param => spec )
end

Instance Method Details

#errorsObject



29
30
31
# File 'lib/param_validator/base.rb', line 29

def errors
  @errors ||= []
end

#full_errorsObject



33
34
35
36
37
# File 'lib/param_validator/base.rb', line 33

def full_errors
  errors.map do |param_and_msg|
    param_and_msg.join( ' ' )
  end
end

#valid?Boolean



18
19
20
21
22
23
24
25
26
27
# File 'lib/param_validator/base.rb', line 18

def valid?
  self.class.specification.each do |param, spec|
    value = fetch_parameter( param )
    if spec[:required] && (value.nil? || value.empty?)
      errors << [param, 'must be present']
    end
  end

  errors.empty?
end