Class: HaveAPI::Validator

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

Overview

Base class for all validators.

All validators can have a short and a full form. The short form is used when default configuration is sufficient. Custom settings can be set using the full form.

The short form means the validator is configured as <option> => <single value>. The full form is <option> => { hash with configuration options }.

It is up to each validator what exactly the short form means and what options can be set. Specify only those options that you wish to override. The only common option is message - the error message sent to the client if the provided value did not pass the validator.

The message can contain %{value}, which is replaced by the actual value that did not pass the validator.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts) ⇒ Validator

Returns a new instance of Validator.



55
56
57
# File 'lib/haveapi/validator.rb', line 55

def initialize(key, opts)
  reconfigure(key, opts)
end

Instance Attribute Details

#messageObject

Returns the value of attribute message.



53
54
55
# File 'lib/haveapi/validator.rb', line 53

def message
  @message
end

#paramsObject

Returns the value of attribute params.



53
54
55
# File 'lib/haveapi/validator.rb', line 53

def params
  @params
end

Class Method Details

.name(v = nil) ⇒ Object

Set validator name used in API documentation.



24
25
26
27
28
29
30
31
# File 'lib/haveapi/validator.rb', line 24

def name(v = nil)
  if v
    @name = v

  else
    @name
  end
end

.takes(*opts) ⇒ Object

Specify options this validator takes from the parameter definition.



34
35
36
# File 'lib/haveapi/validator.rb', line 34

def takes(*opts)
  @takes = opts
end

.use(opts) ⇒ Object

Use the validator on given set of options in hash opts. Used options are removed from opts.



45
46
47
48
49
50
# File 'lib/haveapi/validator.rb', line 45

def use(opts)
  keys = opts.keys & @takes

  fail 'too many keys' if keys.size > 1
  new(keys.first, opts.delete(keys.first))
end

.use?(opts) ⇒ Boolean

True if this validator uses any of options in hash opts.

Returns:



39
40
41
# File 'lib/haveapi/validator.rb', line 39

def use?(opts)
  !(opts.keys & @takes).empty?
end

Instance Method Details

#describeObject

Return a hash documenting this validator.

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/haveapi/validator.rb', line 77

def describe
  raise NotImplementedError
end

#reconfigure(key, opts) ⇒ Object



59
60
61
62
63
# File 'lib/haveapi/validator.rb', line 59

def reconfigure(key, opts)
  @key = key
  @opts = opts
  setup
end

#setupObject

Validators should be configured by the given options. This method may be called multiple times, if the validator is reconfigured after it was created.

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/haveapi/validator.rb', line 72

def setup
  raise NotImplementedError
end

#useful?Boolean

Returns:



65
66
67
# File 'lib/haveapi/validator.rb', line 65

def useful?
  @useful.nil? ? true : @useful
end

#valid?(v) ⇒ Boolean

Return true if the value is valid.

Returns:

Raises:

  • (NotImplementedError)


82
83
84
# File 'lib/haveapi/validator.rb', line 82

def valid?(v)
  raise NotImplementedError
end

#validate(v, params) ⇒ Object

Calls method valid?, but before calling it sets instance variable @params. It contains of hash of all other parameters. The validator may use this information as it will.



89
90
91
92
93
94
# File 'lib/haveapi/validator.rb', line 89

def validate(v, params)
  @params = params
  ret = valid?(v)
  @params = nil
  ret
end