preconditions 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -4
- data/VERSION +1 -1
- data/lib/condition_checker.rb +14 -0
- data/lib/preconditions.rb +3 -1
- data/preconditions.gemspec +2 -2
- data/spec/preconditions_dsl_spec.rb +7 -0
- metadata +4 -4
data/README.md
CHANGED
@@ -38,17 +38,17 @@ To use the fluent DSL API use the `check(arg).is {}` form like so:
|
|
38
38
|
end
|
39
39
|
|
40
40
|
Note that there is less opportunity for custom messaging in the fluent API. However, a second argument to `check` can
|
41
|
-
be supplied to add the argument name to any raised errors:
|
41
|
+
be supplied to add the argument name to any raised errors, or one can use the `#named` method to supply a name:
|
42
42
|
|
43
43
|
class MyMath
|
44
44
|
def sqrt(num)
|
45
|
-
Preconditions.check(num
|
45
|
+
Preconditions.check(num).named('num') { is_not_nil and has_type(Integer) and satisfies(">= 0") { arg >= 0 } }
|
46
46
|
num.sqrt
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
In this case, if `num` is the value -10 then an
|
51
|
-
"Argument 'num' must be >= 0, but was -10"
|
50
|
+
In this case, if `num` is the value -10 then an `ArgumentError` will be raised with a message along the lines of
|
51
|
+
`"Argument 'num' must be >= 0, but was -10"`.
|
52
52
|
|
53
53
|
The set of available checks is documented in the `ConditionChecker` documentation.
|
54
54
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/condition_checker.rb
CHANGED
@@ -13,6 +13,20 @@ class ConditionChecker
|
|
13
13
|
@name = name
|
14
14
|
end
|
15
15
|
|
16
|
+
# Set the name of the argument being checked
|
17
|
+
# @param name [String] the name of the parameter being checked
|
18
|
+
# @return argument if a block is given to evaluate, or self if not to allow for further method chaining
|
19
|
+
# @raises if a block is given and the validation rules specified therein are not met
|
20
|
+
def named(name, &block)
|
21
|
+
@name = name
|
22
|
+
if block_given?
|
23
|
+
instance_eval(&block)
|
24
|
+
arg
|
25
|
+
else
|
26
|
+
self
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
16
30
|
# DSL call. Establishes that the checked argument is non-nil.
|
17
31
|
# @raises [ArgumentError] if the argument is nil
|
18
32
|
# @return true
|
data/lib/preconditions.rb
CHANGED
@@ -97,8 +97,10 @@ module Preconditions
|
|
97
97
|
cc = ConditionChecker.new(argument, name)
|
98
98
|
if block_given?
|
99
99
|
cc.instance_eval(&block)
|
100
|
+
argument # return the argument if a block is given to evaluate the check immediately
|
101
|
+
else
|
102
|
+
cc # return the checker if no evaluation block is supplied
|
100
103
|
end
|
101
|
-
argument
|
102
104
|
end
|
103
105
|
end
|
104
106
|
|
data/preconditions.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{preconditions}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = [%q{tucker}]
|
12
|
-
s.date = %q{2011-05-
|
12
|
+
s.date = %q{2011-05-25}
|
13
13
|
s.description = %q{The Preconditions library provides a simple set of methods for checking arguments being passed into a method. Instead of writing custom checks and raising exceptions directly in your code you can use Preconditions to verify basic properties of your arguments (not-nil, satisfying a boolean expression, being of a certain type/duck-type) and raise the appropriate exception for you.
|
14
14
|
}
|
15
15
|
s.email = %q{tucker+rubygems@glyde.com}
|
@@ -56,4 +56,11 @@ describe "dsl expression" do
|
|
56
56
|
}.to raise_exception(ArgumentError, "Argument 'x' must be less than zero")
|
57
57
|
end
|
58
58
|
|
59
|
+
it "should allow for name specification using the 'named' method" do
|
60
|
+
x = 1
|
61
|
+
expect {
|
62
|
+
Preconditions.check(x).named('x') { is_not_nil and satisfies("<= 0") { x <= 0 } }
|
63
|
+
}.to raise_exception(ArgumentError, "Argument 'x' must be <= 0")
|
64
|
+
end
|
65
|
+
|
59
66
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: preconditions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- tucker
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-05-
|
18
|
+
date: 2011-05-25 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|