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 +4 -4
- data/README.md +2 -0
- data/lib/optionable.rb +7 -1
- data/lib/optionable/unknown.rb +42 -0
- data/lib/optionable/version.rb +1 -1
- data/spec/optionable/unknown_spec.rb +17 -0
- data/spec/optionable_spec.rb +9 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6cb2bd9147a5c10dd8ac1a4ad6f8669c12e7fda
|
4
|
+
data.tar.gz: c6da7fc2c7a38e3f00c08d45b15f456cc715b8b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a9b19e8a01ae47087bca5ab6572792d59064e1d381c40f055618d926faf2bf23c123161a3dd10122e7cc1b7b238e3c05ddb926f224553c71a5c317cae58abba
|
7
|
+
data.tar.gz: 0ac9199ffd60222ea6e912db0fbd732110a13b7c3200e187d22848038b29f9cc5017a50899e7096893fe388bd6ce52dedead663566e87fc20c1cff84cd665ce1
|
data/README.md
CHANGED
data/lib/optionable.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/optionable/version.rb
CHANGED
@@ -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
|
data/spec/optionable_spec.rb
CHANGED
@@ -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.
|
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
|