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)
-
- (Hash) datas
readonly
The datas to filter, they shouldn't be modified to guarantee their integrity.
-
- (Hash) filtered
The filtered datas are the datas after they have been filtered (renamed keys for example) by the validator.
-
- (Object) handler
The error handler given to this instance of the validator.
-
- (Hash) processors
readonly
THe processors to process and validate the keys depending on their types.
Instance Method Summary (collapse)
-
- (Object) hash(key, options = {})
Checks if the given key is a hash or not.
-
- (Validator) initialize(datas)
constructor
Constructor of the classe, receiving the datas to validate and filter.
-
- (Object) method_missing(name, *arguments, &block)
Method used to not directly define the different type validation methods, but instead to look for it in the processors list.
- - (Boolean) respond_to?(name, search_private = false)
Constructor Details
- (Validator) initialize(datas)
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
- (Object) method_missing(name, *arguments, &block)
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
- (Hash) datas (readonly)
Returns The datas to filter, they shouldn’t be modified to guarantee their integrity.
|
# File 'lib/kharon/validator.rb', line 7
|
- (Hash) filtered
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
|
- (Object) handler
Returns the error handler given to this instance of the validator.
|
# File 'lib/kharon/validator.rb', line 19
|
- (Hash) processors (readonly)
Returns THe processors to process and validate the keys depending on their types.
|
# File 'lib/kharon/validator.rb', line 11
|
Instance Method Details
- (Object) hash(key, options = {})
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 |
- (Boolean) respond_to?(name, search_private = false)
50 51 52 |
# File 'lib/kharon/validator.rb', line 50 def respond_to?(name, search_private = false) processors.keys.include?(name) ? true : super end |