Module: Deltacloud::Validation

Included in:
BaseDriver::Operation, Sinatra::Rabbit::Operation
Defined in:
lib/deltacloud/validation.rb

Overview

Copyright © 2009,2010 Red Hat, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Classes: Failure, Param

Instance Method Summary collapse

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.



67
68
69
70
71
# File 'lib/deltacloud/validation.rb', line 67

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



73
74
75
# File 'lib/deltacloud/validation.rb', line 73

def each_param(&block)
  params.each_value { |p| yield p }
end

#param(*args) ⇒ Object

Raises:

  • (DuplicateParamException)


53
54
55
56
57
# File 'lib/deltacloud/validation.rb', line 53

def param(*args)
  raise DuplicateParamException if params[args[0]]
  p = Param.new(args)
  params[p.name] = p
end

#paramsObject



59
60
61
62
# File 'lib/deltacloud/validation.rb', line 59

def params
  @params ||= {}
  @params
end

#validate(values) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/deltacloud/validation.rb', line 77

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.options.empty? and
        not p.options.include?(values[p.name])
      raise Failure.new(p, "Parameter #{p.name} has value #{values[p.name]} which is not in #{p.options.join(", ")}")
    end
  end
end