optionable 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 198b619f4a71038fcb8f5d2b7b0beb81d393f408
4
- data.tar.gz: f5d4a0df739bc012faa4e5edd3f646f0c857af6d
3
+ metadata.gz: f6cb2bd9147a5c10dd8ac1a4ad6f8669c12e7fda
4
+ data.tar.gz: c6da7fc2c7a38e3f00c08d45b15f456cc715b8b6
5
5
  SHA512:
6
- metadata.gz: e34e673ddc316c189d90b07e557b70574f6e7f80cb4c234deb002bd8df6cdd9b2ddd697c619ef1557a4effb086aa69c867e61052d7c9e75e982ceebb9cf65318
7
- data.tar.gz: 9bfe6a413779ad23accb7991aef7919cf59cb43a80908f00e4a2b073d1ae0b21e06a9e0712fcfc9023cc1e5263ed4d357b44840df49ed3c5a09d650ee448f5b8
6
+ metadata.gz: 7a9b19e8a01ae47087bca5ab6572792d59064e1d381c40f055618d926faf2bf23c123161a3dd10122e7cc1b7b238e3c05ddb926f224553c71a5c317cae58abba
7
+ data.tar.gz: 0ac9199ffd60222ea6e912db0fbd732110a13b7c3200e187d22848038b29f9cc5017a50899e7096893fe388bd6ce52dedead663566e87fc20c1cff84cd665ce1
data/README.md CHANGED
@@ -46,6 +46,8 @@ end
46
46
 
47
47
  If the options are invalid, an `Optionable::Invalid` error will be raised.
48
48
 
49
+ If an unknown option is provided, an `Optionable::Unknown` error will be
50
+ raised.
49
51
 
50
52
  API Documentation
51
53
  -----------------
@@ -2,6 +2,7 @@
2
2
  require "optionable/any"
3
3
  require "optionable/dsl"
4
4
  require "optionable/invalid"
5
+ require "optionable/unknown"
5
6
  require "optionable/validator"
6
7
  require "optionable/version"
7
8
 
@@ -19,12 +20,17 @@ module Optionable
19
20
  # @param [ Hash ] options The options to validate.
20
21
  #
21
22
  # @raise [ Optionable::Invalid ] If the options are wack.
23
+ # @raise [ Optionable::Unknown ] If an unknown option is passed.
22
24
  #
23
25
  # @since 0.0.0
24
26
  def validate_strict(options)
25
27
  (options || {}).each_pair do |key, value|
26
28
  validator = optionable_validators[key]
27
- validator.validate!(value) if validator
29
+ if validator
30
+ validator.validate!(value)
31
+ else
32
+ raise Unknown.new(key, optionable_validators.keys)
33
+ end
28
34
  end
29
35
  end
30
36
 
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ module Optionable
3
+
4
+ # This exception is raised whenever unknown options are found during
5
+ # validation.
6
+ #
7
+ # @since 0.0.0
8
+ class Unknown < RuntimeError
9
+
10
+ # @!attribute key
11
+ # @return [ Symbol ] The name of the option.
12
+ # @!attribute names
13
+ # @return [ Array<Symbol> ] The list of valid option names.
14
+ attr_reader :key, :names
15
+
16
+ # Initialize the unknown option exception.
17
+ #
18
+ # @example Initialize the exception.
19
+ # Optionable::Unknown.new(:test, [ :read, :write ])
20
+ #
21
+ # @param [ Symbol ] key The name of the option.
22
+ # @param [ Array<Symbol> ] names The valid option names.
23
+ #
24
+ # @since 0.0.0
25
+ def initialize(key, names)
26
+ @key = key
27
+ @names = names
28
+ super(generate_message)
29
+ end
30
+
31
+ private
32
+
33
+ # Generate the message that will be used in the exception.
34
+ #
35
+ # @api private
36
+ #
37
+ # @since 0.0.0
38
+ def generate_message
39
+ "#{key.inspect} is an unknown option. Valid options are: #{names.map(&:inspect).join(", ")}."
40
+ end
41
+ end
42
+ end
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module Optionable
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe Optionable::Unknown do
4
+
5
+ describe "#message" do
6
+
7
+ let(:invalid) do
8
+ described_class.new(:test, [ :first, :second ])
9
+ end
10
+
11
+ it "returns the valid names in the message" do
12
+ expect(invalid.message).to eq(
13
+ ":test is an unknown option. Valid options are: :first, :second."
14
+ )
15
+ end
16
+ end
17
+ end
@@ -43,6 +43,15 @@ describe Optionable do
43
43
  end
44
44
  end
45
45
 
46
+ context "when options exist that are not defined" do
47
+
48
+ it "raises an error" do
49
+ expect {
50
+ Model.new(something: :else)
51
+ }.to raise_error
52
+ end
53
+ end
54
+
46
55
  context "when the options are valid" do
47
56
 
48
57
  context "when options are allowed specific values" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optionable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
@@ -20,6 +20,7 @@ files:
20
20
  - lib/optionable/any.rb
21
21
  - lib/optionable/dsl.rb
22
22
  - lib/optionable/invalid.rb
23
+ - lib/optionable/unknown.rb
23
24
  - lib/optionable/validator.rb
24
25
  - lib/optionable/version.rb
25
26
  - lib/optionable.rb
@@ -28,6 +29,7 @@ files:
28
29
  - Rakefile
29
30
  - spec/optionable/any_spec.rb
30
31
  - spec/optionable/invalid_spec.rb
32
+ - spec/optionable/unknown_spec.rb
31
33
  - spec/optionable/validator_spec.rb
32
34
  - spec/optionable_spec.rb
33
35
  - spec/spec_helper.rb
@@ -57,6 +59,7 @@ summary: Robust options validation for methods.
57
59
  test_files:
58
60
  - spec/optionable/any_spec.rb
59
61
  - spec/optionable/invalid_spec.rb
62
+ - spec/optionable/unknown_spec.rb
60
63
  - spec/optionable/validator_spec.rb
61
64
  - spec/optionable_spec.rb
62
65
  - spec/spec_helper.rb