simplecheck 0.9 → 1.0rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -7
- data/examples/argument_example.rb +1 -0
- data/examples/block_example.rb +1 -0
- data/examples/case_equality_check.rb +1 -0
- data/lib/simplecheck.rb +14 -29
- data/lib/simplecheck/version.rb +1 -1
- data/simplecheck.gemspec +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a928fd88137669a29e16e814e1d21af7e6403f3f
|
4
|
+
data.tar.gz: 547b3655c4efdbf9ab4e1ef019f51fcc224c537f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c77b199bbbc3308e07b0348f6f7c013cc473376b62930a715ba31b249cff9a55f291cfcb658d8c756c5f929abf0a63bd453f1524a85397619b20535ab98f9ea8
|
7
|
+
data.tar.gz: 1cb867dc97e7dba604956d0432198304990be0d0a024deca114350a47c263a75c41d1a38260ab3e663a133d2fdebcd0c9e00b84ef08eb548df6d8907b247bf3e
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
Simplecheck
|
2
2
|
===========
|
3
3
|
|
4
|
-
Simplecheck is a property checking API for Ruby designed
|
4
|
+
Simplecheck is a lightweight property checking API for Ruby designed to quickly check arguments. Once included into a class it provides the `check` instance method which takes arguments and a condition to check them against.
|
5
5
|
|
6
|
-
If a check fails
|
6
|
+
If a check fails a `Simplecheck::CheckFailed` exception is raised.
|
7
7
|
|
8
8
|
Usage
|
9
9
|
-----
|
@@ -37,15 +37,24 @@ Simplecheck currently supports three different check methods:
|
|
37
37
|
|
38
38
|
### Expression Check
|
39
39
|
|
40
|
-
In the simplest case `check` takes an expression as an argument. If the expression evaluates to `nil` or `false` it will fail.
|
40
|
+
In the simplest case `check` takes an expression as an argument. If the expression evaluates to `nil` or `false` it will fail.
|
41
41
|
|
42
|
-
|
42
|
+
def calculate_percentage( score, total )
|
43
|
+
check( total > 0 )
|
44
|
+
100.0 * score / total
|
45
|
+
end
|
43
46
|
|
44
47
|
### Case Equality (===) Check
|
45
48
|
|
46
49
|
If two or more arguments are given without a block, then the last argument becomes the condition against which the previous arguments are checked. To accomplish this the condition argument should implement the case equality operator (`===` or threequal) in a logical manner.
|
47
50
|
|
48
|
-
|
51
|
+
def greatest_common_divisor( a, b )
|
52
|
+
check( a, b, Numeric )
|
53
|
+
# GCD Algorithm
|
54
|
+
end
|
55
|
+
|
56
|
+
If a class does not alias or implement it's own version of `===` it has the same functionality as `==`. The following Ruby Core classes already alias `===` to various instance methods.
|
57
|
+
|
49
58
|
|
50
59
|
#### Class
|
51
60
|
|
@@ -69,7 +78,7 @@ If a class does not alias or implement it's own version of `===` it has the same
|
|
69
78
|
|
70
79
|
`===` is aliased to `call`:
|
71
80
|
|
72
|
-
check( password, password_confirmation,
|
81
|
+
check( password, password_confirmation , ->(p){ !Dict.lookup( p )})
|
73
82
|
|
74
83
|
#### Custom Check Object
|
75
84
|
|
@@ -84,7 +93,7 @@ For example to check whether a set of points is inside a given polygon we would
|
|
84
93
|
A block can be passed to `check`, with the arguments passed to `check` then passed individually to the block:
|
85
94
|
|
86
95
|
check( a, b, c ) do |n|
|
87
|
-
n.
|
96
|
+
n.odd?
|
88
97
|
end
|
89
98
|
|
90
99
|
This is syntactic sugar for the Proc Case Equality check.
|
@@ -94,6 +103,11 @@ Multiple Arguments
|
|
94
103
|
|
95
104
|
Case Equality and Block checks can be called with multiple arguments, with each argument being checked individually against the condition:
|
96
105
|
|
106
|
+
check( i, j, k, Integer )
|
107
|
+
check( a, b, c ) do |n|
|
108
|
+
n.even?
|
109
|
+
end
|
110
|
+
|
97
111
|
License
|
98
112
|
-------
|
99
113
|
Simplecheck is released under the BSD License.
|
data/examples/block_example.rb
CHANGED
data/lib/simplecheck.rb
CHANGED
@@ -4,55 +4,40 @@ require 'simplecheck/check_failed'
|
|
4
4
|
module Simplecheck
|
5
5
|
def check( *arguments, &block )
|
6
6
|
error_message = if block_given?
|
7
|
-
|
7
|
+
Simplecheck.check_arguments_with_block( arguments, block )
|
8
8
|
else
|
9
|
-
|
9
|
+
Simplecheck.check_arguments( arguments )
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
__handle_failure( error_message )
|
14
|
-
else
|
15
|
-
__handle_return_arguments( arguments )
|
16
|
-
end
|
12
|
+
error_message ? Simplecheck.handle_failure( error_message ) : true
|
17
13
|
end
|
18
14
|
|
19
|
-
|
20
|
-
private
|
21
|
-
def __check_arguments( arguments )
|
15
|
+
def Simplecheck.check_arguments( arguments )
|
22
16
|
case arguments.size
|
23
17
|
when 1
|
24
|
-
|
18
|
+
Simplecheck.check_expression( arguments[ 0 ])
|
25
19
|
else
|
26
|
-
|
20
|
+
Simplecheck.check_case_equality( *arguments )
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
30
|
-
def
|
31
|
-
|
24
|
+
def Simplecheck.check_arguments_with_block( arguments, block )
|
25
|
+
Simplecheck.check_arguments(( arguments + [ block ]))
|
32
26
|
end
|
33
27
|
|
34
|
-
def
|
28
|
+
def Simplecheck.check_expression( expression )
|
35
29
|
if !expression
|
36
|
-
'Condition is not
|
30
|
+
'Condition is not satisfied'
|
37
31
|
end
|
38
32
|
end
|
39
33
|
|
40
|
-
def
|
41
|
-
if invalid_argument = arguments.find{ |argument| !(
|
42
|
-
"#{ invalid_argument } does not satisfy #{
|
34
|
+
def Simplecheck.check_case_equality( *arguments, check_argument )
|
35
|
+
if invalid_argument = arguments.find{ |argument| !( check_argument === argument )}
|
36
|
+
"#{ invalid_argument } does not satisfy #{ check_argument }"
|
43
37
|
end
|
44
38
|
end
|
45
39
|
|
46
|
-
def
|
40
|
+
def Simplecheck.handle_failure( message )
|
47
41
|
raise Simplecheck::CheckFailed.new( message )
|
48
42
|
end
|
49
|
-
|
50
|
-
def __handle_return_arguments( arguments )
|
51
|
-
case arguments.size
|
52
|
-
when 1
|
53
|
-
arguments[0]
|
54
|
-
else
|
55
|
-
arguments
|
56
|
-
end
|
57
|
-
end
|
58
43
|
end
|
data/lib/simplecheck/version.rb
CHANGED
data/simplecheck.gemspec
CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ['Farrel Lifson']
|
10
10
|
s.email = ['farrel.lifson@aimred.com']
|
11
|
-
s.homepage = 'http://
|
11
|
+
s.homepage = 'http://github.com/farrel/simplecheck'
|
12
12
|
s.summary = 'Simple property checking for Ruby'
|
13
13
|
s.description = 'Simple property checking for Ruby'
|
14
14
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simplecheck
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Farrel Lifson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simple property checking for Ruby
|
14
14
|
email:
|
@@ -33,7 +33,7 @@ files:
|
|
33
33
|
- simplecheck.gemspec
|
34
34
|
- test/simplecheck_test.rb
|
35
35
|
- test/test_helper.rb
|
36
|
-
homepage: http://
|
36
|
+
homepage: http://github.com/farrel/simplecheck
|
37
37
|
licenses: []
|
38
38
|
metadata: {}
|
39
39
|
post_install_message:
|
@@ -47,9 +47,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
47
|
version: '0'
|
48
48
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
49
|
requirements:
|
50
|
-
- - '
|
50
|
+
- - '>'
|
51
51
|
- !ruby/object:Gem::Version
|
52
|
-
version:
|
52
|
+
version: 1.3.1
|
53
53
|
requirements: []
|
54
54
|
rubyforge_project: rcap
|
55
55
|
rubygems_version: 2.0.3
|