validate_options 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,60 @@
1
+ = ValidateOptions
2
+
3
+ This is a tiny Ruby snippet that provides a smarter way to validate
4
+ 'options'-like hashes (a.k.a. named arguments).
5
+
6
+ For example:
7
+
8
+ require 'validate_options'
9
+
10
+ def foo(text, options = {})
11
+ # Only :align and :color are allowed.
12
+ validate_options options, :align, :color
13
+
14
+ text
15
+ end
16
+
17
+ # Disabled by default.
18
+ ValidateOptions.enable!
19
+
20
+ foo("Hello")
21
+ # => "Hello"
22
+
23
+ foo("Hello", :align => :left)
24
+ # => "Hello"
25
+
26
+ foo("Hello", :colour => :red)
27
+ # ArgumentError: Invalid options provided: :colour.
28
+
29
+ Using a block for more fine-grained control over arguments:
30
+
31
+ def bar(text, options = {})
32
+ validate_options options, :align do |op|
33
+ raise ":align should be either :left or :right" if op[:align] && ![:left, :right].include?(op[:align])
34
+ end
35
+ end
36
+
37
+ bar("Hello", :align => :left)
38
+ # => "Hello"
39
+
40
+ bar("Hello", :align => :center)
41
+ # ArgumentError: :align should be either :left or :right
42
+
43
+ Valid options can also be specified outside the method:
44
+
45
+ class Formatter
46
+ def print(text, options = {})
47
+ puts text
48
+ end
49
+
50
+ validate_options_for :print, :align, :color
51
+ end
52
+
53
+ Formatter.new.print("Hello")
54
+ # => "Hello"
55
+
56
+ Formatter.new.print("Hello", :align => :left)
57
+ # => "Hello"
58
+
59
+ Formatter.new.print("Hello", :colour => :red)
60
+ # ArgumentError: Invalid options provided: :colour.
@@ -0,0 +1,49 @@
1
+ module ValidateOptions
2
+ def self.disable!
3
+ Kernel.module_eval do
4
+ def validate_options(hash, *keys)
5
+ # no-op
6
+ end
7
+ end
8
+ end
9
+
10
+ def self.enable!
11
+ Kernel.module_eval do
12
+ def validate_options(hash, *keys)
13
+ return unless hash
14
+ extra = hash.keys - keys
15
+ raise ArgumentError, "Invalid options provided: #{extra.map {|k| k.inspect }.join(', ')}." unless extra.empty?
16
+
17
+ if block_given?
18
+ begin
19
+ yield(hash)
20
+ rescue ArgumentError
21
+ raise
22
+ rescue Exception => e
23
+ raise ArgumentError, e
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.included(base)
31
+ base.class_eval <<-EOS
32
+ def validate_options_for(method, *args)
33
+ alias_method "\#{method}_without_options_validation".to_sym, method
34
+
35
+ module_eval <<-EOIS
36
+ def \#{method}(*args)
37
+ validate_options(args.last, *\#{args.inspect})
38
+ \#{method}_without_options_validation(*args)
39
+ end
40
+ EOIS
41
+ end
42
+ EOS
43
+ end
44
+
45
+ # Disable by default.
46
+ disable!
47
+ end
48
+
49
+ Class.send(:include, ValidateOptions)
@@ -0,0 +1,45 @@
1
+ module ValidateOptions
2
+ module Spec
3
+ module Matchers
4
+ class ValidateOptionsMatcher #:nodoc:
5
+ def initialize(accepted_options)
6
+ @accepted_options = accepted_options
7
+ end
8
+
9
+ def matches?(target)
10
+ accepted = @accepted_options.all? do |opt|
11
+ begin
12
+ target.call(opt => nil)
13
+ true
14
+ rescue ArgumentError
15
+ false
16
+ end
17
+ end
18
+
19
+ rejected = begin
20
+ target.call("___very_odd_probably_invalid_key" => nil)
21
+ false
22
+ rescue ArgumentError
23
+ true
24
+ end
25
+
26
+ accepted && rejected
27
+ end
28
+
29
+ def failure_message
30
+ "expected block to validate options."
31
+ end
32
+ end
33
+
34
+ # :call-seq:
35
+ # should validate_options(:color)
36
+ #
37
+ # == Examples
38
+ #
39
+ # lambda {|options| Formatter.text('Hello', options) }.should validate_options(:color)
40
+ def validate_options(*accepted_options)
41
+ ValidateOptionsMatcher.new(accepted_options)
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validate_options
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Damian Janowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-19 00:00:00 -03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: ""
17
+ email: damian.janowski@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ files:
25
+ - lib/validate_options/spec.rb
26
+ - lib/validate_options.rb
27
+ - README.rdoc
28
+ has_rdoc: true
29
+ homepage:
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --line-numbers
33
+ - --inline-source
34
+ - --title
35
+ - tti
36
+ - --main
37
+ - README.rdoc
38
+ require_paths:
39
+ - lib
40
+ - lib/validate_options
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: Hash validation for 'options'-like methods.
60
+ test_files: []
61
+