Class: Kharon::Validator

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

Overview

The validator is the main class of Kharon, it validates a hash given a structure.

Author:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Validator) initialize(datas)

Constructor of the classe, receiving the datas to validate and filter.

Examples:

create a new instance of validator.

@validator = Kharon::Validator.new({key: "value"})

Parameters:

  • datas (Hash)

    the datas to validate in the validator.



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.

Parameters:

  • name (String)

    the name of the not found method.

  • arguments (Array)

    the arguments passed to the not found method when it’s called.

  • block (Proc)

    the block that might have been passed to the not found method when it’s called.



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.

Returns:

  • (Hash)

    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.

Returns:

  • (Hash)

    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.

Returns:

  • (Object)

    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.

Returns:

  • (Hash)

    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.

Examples:

Validates a key so it has to be a hash, and checks if it has some keys.

@validator.date(:a_hash, has_keys: [:first, :second])

Parameters:

  • key (Object)

    the key about which verify the type.

  • options (Hash) (defaults to: {})

    a hash of options passed to this method (see documentation to know which options pass).



60
61
62
# File 'lib/kharon/validator.rb', line 60

def hash(key, options = {})
  processors[:hash].process(key, options)
end

- (Boolean) respond_to?(name, search_private = false)

Returns:

  • (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