Class: HashParams::BindingValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_params/binding_validator.rb

Instance Method Summary collapse

Instance Method Details

#var(var_name, type, opts = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hash_params/binding_validator.rb', line 10

def var(var_name, type, opts={})
  raise 'Variable name must be a string or symbol' unless (var_name.is_a?(String) || var_name.is_a?(Symbol))
  value    = @binding.local_variable_get var_name
  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, opts, &Proc.new)
                else
                  HashParams::HashValidator.new.validate_hash(value, opts)
                end
              else
                HashParams.validate value, type,  opts
              end
  @binding.local_variable_set var_name, new_value
end

#with_binding(&code) ⇒ Object



4
5
6
7
# File 'lib/hash_params/binding_validator.rb', line 4

def with_binding (&code)
  @binding = code.binding
  instance_eval(&code)
end