Module: Rsxml::Util

Included in:
Rsxml, Visitor
Defined in:
lib/rsxml/util.rb

Class Method Summary collapse

Class Method Details

.check_opts(constraints, opts) ⇒ Object

simple option checking, with value constraints and sub-hash checking



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rsxml/util.rb', line 6

def check_opts(constraints, opts)
  opts ||= {}
  opts.each{|k,v| raise "opt not permitted: #{k.inspect}" if !constraints.has_key?(k)}
  Hash[constraints.map do |k,constraint|
         if opts.has_key?(k)
           v = opts[k]
           if constraint.is_a?(Array)
             raise "unknown value for opt #{k.inspect}: #{v.inspect}. permitted values are: #{constraint.inspect}" if !constraint.include?(v)
             [k,v]
           elsif constraint.is_a?(Hash)
             raise "opt #{k.inspect} must be a Hash" if !v.is_a?(Hash)
             [k,check_opts(constraint, v || {})]
           else
             [k,v]
           end
         end
       end]
end