Class: EasyInstaller::Validator
- Inherits:
-
Object
- Object
- EasyInstaller::Validator
- Defined in:
- lib/easy_installer/validator.rb
Overview
Singleton Class which handles input validation
Instance Attribute Summary collapse
-
#inputs_errors ⇒ Object
Returns the value of attribute inputs_errors.
Instance Method Summary collapse
-
#add_validating_function(input, input_ready, code) ⇒ Object
Manualy add validating function to class ==== Attributes *
input- Array of String : list of input names which will be validated by function *input_ready- Array of String: list of input names which have to be ready to run function * &block - Block of code : validating code ==== Example add_validating_function(,[‘path’,‘host’]) do #some code true end. -
#check(inputs) ⇒ Object
Eval each validating functions and returns validated user_input.
-
#clear_checking_functions ⇒ Object
checks is inputs valid def valid? ifcheck_evaled end Removes all validating functions.
-
#import_from_config_by_method(method) ⇒ Object
Automaticaly imports validation function from config by method ==== Attributes *
method- String : name of method. -
#initialize(config) ⇒ Validator
constructor
A new instance of Validator.
Constructor Details
#initialize(config) ⇒ Validator
Returns a new instance of Validator.
33 34 35 |
# File 'lib/easy_installer/validator.rb', line 33 def initialize(config) @config = config end |
Instance Attribute Details
#inputs_errors ⇒ Object
Returns the value of attribute inputs_errors.
32 33 34 |
# File 'lib/easy_installer/validator.rb', line 32 def inputs_errors @inputs_errors end |
Instance Method Details
#add_validating_function(input, input_ready, code) ⇒ Object
Manualy add validating function to class
Attributes
-
input- Array of String : list of input names which will be validated by function -
input_ready- Array of String: list of input names which have to be ready to run function -
&block - Block of code : validating code
Example
add_validating_function(,[‘path’,‘host’]) do #some code true end
58 59 60 61 |
# File 'lib/easy_installer/validator.rb', line 58 def add_validating_function(input, input_ready, code) @checking_functions = Array.new unless @checking_functions @checking_functions << {:input => input, :input_ready=>input_ready, :code => code } end |
#check(inputs) ⇒ Object
Eval each validating functions and returns validated user_input
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/easy_installer/validator.rb', line 64 def check(inputs) lang = @config["lang"] require "easy_installer/modules/#{lang}/validate_helper" require "easy_installer/methods/#{@method}/install_helper" if @method require "easy_installer/methods/#{@method}/validate_helper" if @method # runner = ValidatorRunner.new # runner.inputs = inputs # runner.extend(ModuleValidateHelper) # runner.extend(InstallHelper) if @method # runner.extend(MethodValidateHelper) if @method @inputs_errors = inputs.clone @inputs_errors.each{|key,val| @inputs_errors[key]=nil} @checking_functions.each do |element| begin return false unless (element['code'].call) rescue Exception => e element['input'].each do |key, val| @inputs_errors[key] = e. end return false end end return true end |
#clear_checking_functions ⇒ Object
checks is inputs valid def valid? ifcheck_evaled end Removes all validating functions
95 96 97 |
# File 'lib/easy_installer/validator.rb', line 95 def clear_checking_functions @checking_functions = Array.new end |
#import_from_config_by_method(method) ⇒ Object
Automaticaly imports validation function from config by method
Attributes
* +method+ - String : name of method
40 41 42 43 44 45 46 |
# File 'lib/easy_installer/validator.rb', line 40 def import_from_config_by_method(method) @method = method sandbox = Sandbox.new @config.validating_code(method) sandbox.modules << ValidatorHelper sandbox.eval @checking_functions = sandbox.returned_raw end |