Class: HashParams::HashValidator
- Inherits:
-
Object
- Object
- HashParams::HashValidator
- Defined in:
- lib/hash_params/hash_validator.rb
Instance Method Summary collapse
Instance Method Details
#key(hash_key, type, opts = {}) ⇒ Object Also known as: param
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hash_params/hash_validator.rb', line 54 def key(hash_key, type, opts={}) value = @incoming[hash_key] || @incoming[hash_key.to_s] # if a block is given to the param then it's a recursive call # recursive calls can only be done with a hash new_value = if value.is_a?(Hash) if block_given? #if the param is a hash then the validations are actually options HashParams::HashValidator.new.validate_hash(value, @options, &Proc.new) else HashParams::HashValidator.new.validate_hash(value, opts) end else HashParams.validate value, type, opts end hash_key = opts[:as] if opts[:as] @outgoing.set_key_value(hash_key, new_value, @options[:symbolize_keys], @options[:make_methods]) new_value rescue => e @outgoing.validation_errors << "Error processing key '#{hash_key}': #{e}" # [e.to_s, e.backtrace].join("\n") raise e if @options[:raise_errors] nil end |
#validate_hash(h, options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/hash_params/hash_validator.rb', line 31 def validate_hash(h, ={}) #Hash Validation has to be stateful @incoming = h @outgoing = HPHash.new @options = if block_given? instance_eval(&Proc.new) else #no proc was given this means just pass the hash back as is if @options.empty? @outgoing = @incoming else h.each do |k, v| key k, v.class, @options end end end @outgoing end |