simplecheck 0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2b4972b81b875d8ee3c78f8cf807c36aeffb5f5d
4
+ data.tar.gz: 8a5397331cdce47a1dc42f8eb696d0cbc718b18d
5
+ SHA512:
6
+ metadata.gz: 99c1b54c9c1f3b319835e271ead32230fe82ac592896760841afe812fef13ec5a2f8c93397c1ed44ef78969eac362de415267778c6f89c23b39e0be0ea50445e
7
+ data.tar.gz: ef92f00f785240a4279a6888780a5426fa2b76424a35b8a7c7f4354c321349da3ba7e7453934b380cca20e2d5ea34a021a7e042529b81b0daf25b74558ce780f
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .ruby-version
3
+ doc/
4
+ .yardoc
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ Changelog
2
+ =========
3
+
4
+ 1.0
5
+ ---
6
+ * Initial release
7
+ * Expression, Case Equality and Block checks
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ notification( :terminal_title )
5
+
6
+ guard( :minitest, include: [ 'lib' ]) do
7
+ watch( 'test/simplecheck_test.rb' )
8
+ watch( 'lib/simplecheck.rb' ){ 'test/simplecheck_test.rb' }
9
+ end
data/README.md ADDED
@@ -0,0 +1,111 @@
1
+ Simplecheck
2
+ ===========
3
+
4
+ Simplecheck is a property checking API for Ruby designed for quickly checking arguments. Once included into a class it provides the `check` instance method which takes a list of arguments and a condition to check them against.
5
+
6
+ If a check fails an exception of type `Simplecheck::CheckFailed` is raised.
7
+
8
+ Usage
9
+ -----
10
+
11
+ require 'simplecheck'
12
+
13
+ class Person
14
+ include Simplecheck
15
+
16
+ def initialize( name, age )
17
+ check( name ) # Check name is not nil
18
+ check( age, 18..75 ) # Check age is within range
19
+
20
+ @name = name
21
+ @age = age
22
+ end
23
+ end
24
+
25
+ Person.new( "Joe", 25 ) # No error
26
+ Person.new( nil, 25 ) rescue puts "Name can not be nil"
27
+ Person.new( "Joe", 15 ) rescue puts "Age is out of range"
28
+
29
+ Check Methods
30
+ -------------
31
+
32
+ Simplecheck currently supports three different check methods:
33
+
34
+ * Expression Check
35
+ * Case Equality (===) Check
36
+ * Block Check
37
+
38
+ ### Expression Check
39
+
40
+ In the simplest case `check` takes an expression as an argument. If the expression evaluates to `nil` or `false` it will fail.
41
+
42
+ check( a > 2 )
43
+
44
+ ### Case Equality (===) Check
45
+
46
+ 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
+
48
+ 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. If a class does not alias or implement it's own version of `===` it has the same functionality as `==`.
49
+
50
+ #### Class
51
+
52
+ `===` is aliased to `kind_of?`:
53
+
54
+ check( age, Numeric )
55
+
56
+ #### Range
57
+
58
+ `===` is aliased to `include?`:
59
+
60
+ check( age, 18..75 )
61
+
62
+ #### Regexp
63
+
64
+ `===` is aliased to `match`:
65
+
66
+ check( phone_number, /^\d\d\d-\d\d\d\d$/ )
67
+
68
+ #### Proc
69
+
70
+ `===` is aliased to `call`:
71
+
72
+ check( password, password_confirmation, lambda{ |pwd| !Dictionary.check( pwd )})
73
+
74
+ #### Custom Check Object
75
+
76
+ The default behaviour of `Object#===` is the same as `Object#==`. To customise the behaviour implement your own `Object#===` method.
77
+
78
+ For example to check whether a set of points is inside a given polygon we would implement `Polygon#===` as a [point-in-polygon algorithm](https://en.wikipedia.org/wiki/Point_in_polygon), allowing us to carry out the check using a Polygon instance:
79
+
80
+ check( point_1, point_2, polygon )
81
+
82
+ ### Block Check
83
+
84
+ A block can be passed to `check`, with the arguments passed to `check` then passed individually to the block:
85
+
86
+ check( a, b, c ) do |n|
87
+ n.modulo?(2).zero?
88
+ end
89
+
90
+ This is syntactic sugar for the Proc Case Equality check.
91
+
92
+ Multiple Arguments
93
+ ------------------
94
+
95
+ Case Equality and Block checks can be called with multiple arguments, with each argument being checked individually against the condition:
96
+
97
+ License
98
+ -------
99
+ Simplecheck is released under the BSD License.
100
+
101
+ Copyright 2013 AIMRED CC. All rights reserved.
102
+
103
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
104
+
105
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
106
+
107
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
108
+
109
+ THIS SOFTWARE IS PROVIDED BY AIMRED CC "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AIMRED CC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
110
+
111
+ The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of AIMRED CC.
@@ -0,0 +1,8 @@
1
+ require 'simplecheck'
2
+ include Simplecheck
3
+
4
+ check( 1.odd? )
5
+ check( 5 > 2 )
6
+ check( [ 1, 2, 3 ].include?( 2 ))
7
+
8
+ puts "All passed"
@@ -0,0 +1,11 @@
1
+ require 'simplecheck'
2
+ include Simplecheck
3
+
4
+ # Block passed implicitly to check
5
+ check( 2, 4 ){ |x| x.even? }
6
+
7
+ # Block passed explicity to check
8
+ is_even = lambda{ |x| x.even? }
9
+ check( 2, 4, &is_even )
10
+
11
+ puts "All passed"
@@ -0,0 +1,16 @@
1
+ require 'simplecheck'
2
+ include Simplecheck
3
+
4
+ # Class#=== -> Class#kind_of?
5
+ check( 1, Integer )
6
+
7
+ # Regexp#=== -> Regexp#match
8
+ check( 'aaabbb', /^aaa/ )
9
+
10
+ # Range#=== -> Range#include?
11
+ check( 1, 1..10 )
12
+
13
+ # Proc#=== -> Proc#call
14
+ check( 1, lambda{ |n| n.odd? })
15
+
16
+ puts "All passed"
@@ -0,0 +1,53 @@
1
+ require 'date'
2
+ require 'simplecheck'
3
+
4
+ class Person
5
+ include Simplecheck
6
+ include Comparable
7
+
8
+ attr_accessor( :name, :surname, :date_of_birth )
9
+
10
+ def initialize( name, surname, date_of_birth )
11
+ check( name, surname, String )
12
+ check( date_of_birth, Date )
13
+
14
+ @name = name
15
+ @surname = surname
16
+ @date_of_birth = date_of_birth
17
+ end
18
+
19
+ def <=>( person )
20
+ check( person, Person )
21
+ check( person.date_of_birth )
22
+
23
+ self.date_of_birth <=> person.date_of_birth
24
+ end
25
+ end
26
+
27
+ def try
28
+ begin
29
+ yield
30
+ rescue Simplecheck::CheckFailed => exception
31
+ puts "Check Failed: #{ exception.message }"
32
+ rescue => exception
33
+ puts "EXCEPTION: #{ exception.message }"
34
+ end
35
+ end
36
+
37
+ # date_of_birth is not a Date
38
+ try{ Person.new( 'Bob', 'Roberts', '1980-01-01' )}
39
+
40
+ bob = Person.new( 'Bob', 'Roberts', Date.civil( 1970, 1, 1 ))
41
+ joe = Person.new( 'Joe', 'Josephs', Date.civil( 1980, 1, 1 ))
42
+
43
+ # 1 is not a Person
44
+ try{ bob > 1 }
45
+
46
+ if joe > bob
47
+ puts "Joe > Bob"
48
+ end
49
+
50
+ bob.date_of_birth = nil
51
+
52
+ # date_of_birth is not present
53
+ try{ joe > bob }
@@ -0,0 +1,58 @@
1
+ require 'simplecheck/version'
2
+ require 'simplecheck/check_failed'
3
+
4
+ module Simplecheck
5
+ def check( *arguments, &block )
6
+ error_message = if block_given?
7
+ __check_arguments_with_block( arguments, block )
8
+ else
9
+ __check_arguments( arguments )
10
+ end
11
+
12
+ if error_message
13
+ __handle_failure( error_message )
14
+ else
15
+ __handle_return_arguments( arguments )
16
+ end
17
+ end
18
+
19
+
20
+ private
21
+ def __check_arguments( arguments )
22
+ case arguments.size
23
+ when 1
24
+ __check_expression( arguments[ 0 ])
25
+ else
26
+ __check_case_equality( *arguments )
27
+ end
28
+ end
29
+
30
+ def __check_arguments_with_block( arguments, block )
31
+ __check_arguments(( arguments + [ block ]))
32
+ end
33
+
34
+ def __check_expression( expression )
35
+ if !expression
36
+ 'Condition is not true'
37
+ end
38
+ end
39
+
40
+ def __check_case_equality( *arguments, receiver )
41
+ if invalid_argument = arguments.find{ |argument| !( receiver === argument )}
42
+ "#{ invalid_argument } does not satisfy #{ receiver }"
43
+ end
44
+ end
45
+
46
+ def __handle_failure( message )
47
+ raise Simplecheck::CheckFailed.new( message )
48
+ 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
+ end
@@ -0,0 +1,3 @@
1
+ module Simplecheck
2
+ class CheckFailed < StandardError; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module Simplecheck
2
+ VERSION = '0.9'
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.unshift File.expand_path('../lib', __FILE__)
3
+ require 'simplecheck/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'simplecheck'
7
+ s.version = Simplecheck::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Farrel Lifson']
10
+ s.email = ['farrel.lifson@aimred.com']
11
+ s.homepage = 'http://www.aimred.com/projects/simplecheck'
12
+ s.summary = 'Simple property checking for Ruby'
13
+ s.description = 'Simple property checking for Ruby'
14
+
15
+ s.rubyforge_project = 'rcap'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- test/*`.split("\n")
19
+ s.require_paths = ['lib']
20
+
21
+ s.extra_rdoc_files = ['README.md','CHANGELOG.md']
22
+ end
@@ -0,0 +1,98 @@
1
+ require 'test_helper'
2
+
3
+ class Foo
4
+ include Simplecheck
5
+
6
+ def expression_check( a )
7
+ check( a )
8
+ end
9
+
10
+ def case_equality_check( a )
11
+ check( a, Integer )
12
+ end
13
+
14
+ def case_equality_check_multiple_arguments( *arguments )
15
+ check( *arguments, Integer )
16
+ end
17
+
18
+ def block_check( a )
19
+ check( a ){ |x| x.modulo( 2 ).zero? }
20
+ end
21
+
22
+ def block_check_multiple_arguments( a, b, c )
23
+ check( a, b, c ){ |x| x.even? }
24
+ end
25
+
26
+ def lambda_argument_check( a )
27
+ check_lambda = lambda{ |x| x.even? }
28
+ check( a, check_lambda )
29
+ end
30
+
31
+ def lambda_block_check( a )
32
+ check_lambda = lambda{ |x| x.even? }
33
+ check( a, &check_lambda )
34
+ end
35
+ end
36
+
37
+ class TestSimplecheck < MiniTest::Test
38
+
39
+ def setup
40
+ @foo = Foo.new
41
+ end
42
+
43
+ def test_expression_check_true
44
+ assert( @foo.expression_check( 1 ))
45
+ end
46
+
47
+ def test_expression_check_raises_exception
48
+ assert_raises( Simplecheck::CheckFailed ){ @foo.expression_check( nil )}
49
+ end
50
+
51
+ def test_case_equality_check_true
52
+ assert( @foo.case_equality_check( 1 ))
53
+ end
54
+
55
+ def test_case_equality_check_raises_exception
56
+ assert_raises( Simplecheck::CheckFailed ){ @foo.case_equality_check( '1' )}
57
+ end
58
+
59
+ def test_case_equality_check_multiple_arguments
60
+ assert( @foo.case_equality_check_multiple_arguments( 1, 2 ))
61
+ end
62
+
63
+ def test_case_equality_check_multiple_arguments_raises_exception
64
+ assert_raises( Simplecheck::CheckFailed ){ @foo.case_equality_check_multiple_arguments( 1, '2' )}
65
+ end
66
+
67
+ def test_block_check_true
68
+ assert( @foo.block_check( 2 ))
69
+ end
70
+
71
+ def test_block_check_raises_exception
72
+ assert_raises( Simplecheck::CheckFailed ){ @foo.block_check( 1 )}
73
+ end
74
+
75
+ def test_block_check_multiple_arguments_true
76
+ assert( @foo.block_check_multiple_arguments( 2, 2, 2 ))
77
+ end
78
+
79
+ def test_block_check_multiple_arguments_exception
80
+ assert_raises( Simplecheck::CheckFailed ){ @foo.block_check_multiple_arguments( 2, 2, 1 )}
81
+ end
82
+
83
+ def test_lambda_argument_check_true
84
+ assert( @foo.lambda_argument_check( 2 ))
85
+ end
86
+
87
+ def test_lambda_argument_check_raises_exception
88
+ assert_raises( Simplecheck::CheckFailed ){ @foo.lambda_argument_check( 1 )}
89
+ end
90
+
91
+ def test_lambda_block_check_true
92
+ assert( @foo.lambda_block_check( 2 ))
93
+ end
94
+
95
+ def test_lambda_block_check_raises_exception
96
+ assert_raises( Simplecheck::CheckFailed ){ @foo.lambda_block_check( 1 )}
97
+ end
98
+ end
@@ -0,0 +1,2 @@
1
+ require 'minitest/autorun'
2
+ require 'simplecheck'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simplecheck
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.9'
5
+ platform: ruby
6
+ authors:
7
+ - Farrel Lifson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple property checking for Ruby
14
+ email:
15
+ - farrel.lifson@aimred.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.md
20
+ - CHANGELOG.md
21
+ files:
22
+ - .gitignore
23
+ - CHANGELOG.md
24
+ - Guardfile
25
+ - README.md
26
+ - examples/argument_example.rb
27
+ - examples/block_example.rb
28
+ - examples/case_equality_check.rb
29
+ - examples/person_example.rb
30
+ - lib/simplecheck.rb
31
+ - lib/simplecheck/check_failed.rb
32
+ - lib/simplecheck/version.rb
33
+ - simplecheck.gemspec
34
+ - test/simplecheck_test.rb
35
+ - test/test_helper.rb
36
+ homepage: http://www.aimred.com/projects/simplecheck
37
+ licenses: []
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project: rcap
55
+ rubygems_version: 2.0.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Simple property checking for Ruby
59
+ test_files:
60
+ - test/simplecheck_test.rb
61
+ - test/test_helper.rb
62
+ has_rdoc: