Class: HaveAPI::Client::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/client/validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, value, other_params) ⇒ Validator

Returns a new instance of Validator.



31
32
33
34
35
# File 'lib/haveapi/client/validator.rb', line 31

def initialize(opts, value, other_params)
  @opts = opts
  @value = value
  @params = other_params.params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



29
30
31
# File 'lib/haveapi/client/validator.rb', line 29

def params
  @params
end

#valueObject (readonly)

Returns the value of attribute value.



29
30
31
# File 'lib/haveapi/client/validator.rb', line 29

def value
  @value
end

Class Method Details

.name(v) ⇒ Object



6
7
8
# File 'lib/haveapi/client/validator.rb', line 6

def name(v)
  Validator.register(v, self)
end

.register(name, klass) ⇒ Object



10
11
12
13
# File 'lib/haveapi/client/validator.rb', line 10

def register(name, klass)
  @validators ||= {}
  @validators[name] = klass
end

.validate(validators, param, other_params) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/haveapi/client/validator.rb', line 15

def validate(validators, param, other_params)
  ret = []

  validators.each do |name, desc|
    raise "unsupported validator '#{name}'" if @validators[name].nil?

    v = @validators[name].new(desc, param, other_params)
    ret.concat(v.errors) unless v.valid?
  end

  ret.empty? ? true : ret
end

Instance Method Details

#errorsObject



37
38
39
# File 'lib/haveapi/client/validator.rb', line 37

def errors
  @errors || [format(opts[:message], value: value)]
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/haveapi/client/validator.rb', line 41

def valid?
  raise NotImplementedError
end