Class: Kharon::Processors::HashProcessor

Inherits:
Kharon::Processor show all
Defined in:
lib/kharon/processors/hash_processor.rb

Overview

Processor to validate hashes. It has the :has_keys and :contains options with the default ones.

Author:

Instance Attribute Summary

Attributes inherited from Kharon::Processor

#validator

Instance Method Summary (collapse)

Methods inherited from Kharon::Processor

#initialize

Constructor Details

This class inherits a constructor from Kharon::Processor

Instance Method Details

- (Object) process(key, options = {})

Checks if the given key is a datetime or not.

Examples:

Validates a key so it has to be a datetime, and depends on two other keys.

@validator.datetime(:a_datetime, dependencies: [:another_key, :a_third_key])

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).



15
16
17
18
# File 'lib/kharon/processors/hash_processor.rb', line 15

def process(key, options = {})
  before_all(key, options)
  is_typed?(key, Hash) ? store(key, ->(item){Hash.try_convert(item)}, options) : raise_type_error(key, "Hash")
end

- (Object) store(key, process, options)

Stores an array after verifying that it contains the values given in the contains? option.

Parameters:

  • key (Object)

    the key associated with the value to store in the filtered datas.

  • process (Proc)

    a process (lambda) to execute on the initial value. Must contain strictly one argument.

  • options (Hash)

    the options applied to the initial value.



24
25
26
27
28
# File 'lib/kharon/processors/hash_processor.rb', line 24

def store(key, process, options)
  has_keys?(key, options[:has_keys]) if(options.has_key?(:has_keys))
  contains?(validator.filtered, validator.datas[key].values, options[:contains]) if(options.has_key?(:contains))
  super(key, process, options)
end