Class: Kharon::Processors::TextProcessor

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

Overview

Processor to validate simple strings. It has the :regex option plus 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 not-empty string or not.

Examples:

Validates a key so it has to be a string, and seems like and email address (not sure of the regular expression though).

@validator.text(:an_email, regex: "[a-zA-Z]+@[a-zA-Z]+\.[a-zA-Z]{2-4}")

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/text_processor.rb', line 15

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

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

Stores a string after verifying that it respects a regular expression given in parameter.

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
# File 'lib/kharon/processors/text_processor.rb', line 24

def store(key, process, options)
  match_regex?(key, validator.datas[key], options[:regex]) if(options.has_key?(:regex))
  super(key, process, options)
end