Module: Deltacloud::Validation
- Included in:
- BaseDriver::Operation, Sinatra::Rabbit::Operation
- Defined in:
- lib/deltacloud/validation.rb
Defined Under Namespace
Instance Method Summary collapse
-
#add_params(new) ⇒ Object
Add the parameters in hash
new
to already existing parameters. - #each_param(&block) ⇒ Object
- #param(*args) ⇒ Object
- #params ⇒ Object
- #validate(values) ⇒ Object
Instance Method Details
#add_params(new) ⇒ Object
Add the parameters in hash new
to already existing parameters. If new
contains a parameter with an already existing name, the old definition is clobbered.
49 50 51 52 53 |
# File 'lib/deltacloud/validation.rb', line 49 def add_params(new) # We do not check for duplication on purpose: multiple calls # to add_params should be cumulative new.each { |p| @params[p.name] = p } end |
#each_param(&block) ⇒ Object
55 56 57 |
# File 'lib/deltacloud/validation.rb', line 55 def each_param(&block) params.each_value { |p| yield p } end |
#param(*args) ⇒ Object
35 36 37 38 39 |
# File 'lib/deltacloud/validation.rb', line 35 def param(*args) raise DuplicateParamException if params[args[0]] p = Param.new(args) params[p.name] = p end |
#params ⇒ Object
41 42 43 44 |
# File 'lib/deltacloud/validation.rb', line 41 def params @params ||= {} @params end |
#validate(values) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/deltacloud/validation.rb', line 59 def validate(values) each_param do |p| if p.required? and not values[p.name] raise Failure.new(p, "Required parameter #{p.name} not found") end if values[p.name] and not p..empty? and not p..include?(values[p.name]) raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p..join(", ")}") end end end |