Module: ValidateOptions

Defined in:
lib/validate_options.rb,
lib/validate_options/spec.rb

Defined Under Namespace

Modules: Spec

Class Method Summary collapse

Class Method Details

.disable!Object



2
3
4
5
6
7
8
# File 'lib/validate_options.rb', line 2

def self.disable!
  Kernel.module_eval do
    def validate_options(hash, *keys)
      # no-op
    end
  end
end

.enable!Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/validate_options.rb', line 10

def self.enable!
  Kernel.module_eval do
    def validate_options(hash, *keys)
      return unless hash
      extra = hash.keys - keys
      raise ArgumentError, "Invalid options provided: #{extra.map {|k| k.inspect }.join(', ')}." unless extra.empty?

      if block_given?
        begin
          yield(hash)
        rescue ArgumentError
          raise
        rescue Exception => e
          raise ArgumentError, e
        end
      end
    end
  end
end

.included(base) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/validate_options.rb', line 30

def self.included(base)
  base.class_eval <<-EOS
  def validate_options_for(method, *args)
    alias_method "\#{method}_without_options_validation".to_sym, method

    module_eval <<-EOIS
      def \#{method}(*args)
        validate_options(args.last, *\#{args.inspect})
        \#{method}_without_options_validation(*args)
      end
    EOIS
  end
  EOS
end