Class: Kharon::Validator
- Inherits:
-
Object
- Object
- Kharon::Validator
- Defined in:
- lib/kharon/validator.rb
Overview
The validator is the main class of Kharon, it validates a hash given a structure.
Instance Attribute Summary collapse
-
#datas ⇒ Hash
readonly
The datas to filter, they shouldn’t be modified to guarantee their integrity.
-
#filtered ⇒ Hash
The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
-
#handler ⇒ Object
The error handler given to this instance of the validator.
-
#processors ⇒ Hash
readonly
THe processors to process and validate the keys depending on their types.
Instance Method Summary collapse
-
#hash(key, options = {}) ⇒ Object
Checks if the given key is a hash or not.
-
#initialize(datas) ⇒ Validator
constructor
Constructor of the classe, receiving the datas to validate and filter.
-
#method_missing(name, *arguments, &block) ⇒ Object
Method used to not directly define the different type validation methods, but instead to look for it in the processors list.
- #respond_to?(name, search_private = false) ⇒ Boolean
Constructor Details
#initialize(datas) ⇒ Validator
Constructor of the classe, receiving the datas to validate and filter.
27 28 29 30 31 32 |
# File 'lib/kharon/validator.rb', line 27 def initialize(datas) @datas = Hash[datas.map { |k, v| [k.to_sym, v] }] @processors = Hash[Kharon.processors.map { |name, classname| [name, classname.new(self)] }] @filtered = Hash.new @handler = Kharon.errors_handler end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, &block) ⇒ Object
Method used to not directly define the different type validation methods, but instead to look for it in the processors list.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/kharon/validator.rb', line 38 def method_missing(name, *arguments, &block) if respond_to? name if arguments.count == 1 processors[name].process(arguments[0]) elsif arguments.count == 2 processors[name].process(arguments[0], arguments[1]) end else super end end |
Instance Attribute Details
#datas ⇒ Hash (readonly)
Returns The datas to filter, they shouldn’t be modified to guarantee their integrity.
|
|
# File 'lib/kharon/validator.rb', line 7
|
#filtered ⇒ Hash
Returns The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
|
|
# File 'lib/kharon/validator.rb', line 15
|
#handler ⇒ Object
Returns the error handler given to this instance of the validator.
|
|
# File 'lib/kharon/validator.rb', line 19
|
#processors ⇒ Hash (readonly)
Returns THe processors to process and validate the keys depending on their types.
|
|
# File 'lib/kharon/validator.rb', line 11
|
Instance Method Details
#hash(key, options = {}) ⇒ Object
Checks if the given key is a hash or not. This method MUST be defined to override the #hash method with these parameters.
60 61 62 |
# File 'lib/kharon/validator.rb', line 60 def hash(key, = {}) processors[:hash].process(key, ) end |
#respond_to?(name, search_private = false) ⇒ Boolean
50 51 52 |
# File 'lib/kharon/validator.rb', line 50 def respond_to?(name, search_private = false) processors.keys.include?(name) ? true : super end |