contract 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 015f97c2285dac2ec81370f391be68fe2f0e0e12
4
+ data.tar.gz: 3769a9e66a2c4dd76426f1df7c101689d69d1983
5
+ SHA512:
6
+ metadata.gz: 4f01f554ff61a8002c853b699b9ea393fac00585560143e396fdad79b6953220e893a9b21b298962ca5eac3158af7719ae0b73f2dbbacbe916a220dd099f4cc3
7
+ data.tar.gz: b28b4edaca9aa181c37189d8abd1613d03857e67379c6b3d0595841f11fb4ba54f3cd788160b2a36dfc4c42669304cf470b9c0dc965852c7aae82fb9eb0bdecf
data/.gems ADDED
@@ -0,0 +1 @@
1
+ cutest -v 1.2.1
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2014 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Contract helper
2
+
3
+ This module provides a way to define contracts in functions, and
4
+ return a value based on a series of user defined verifications.
5
+
6
+ ## Usage
7
+
8
+ The functions of a contract can be used directly from the
9
+ `Contract` module:
10
+
11
+ ``` ruby
12
+ class Foo
13
+ def foo(a, b)
14
+ Contract.define do
15
+ Contract.verify(Numeric === a)
16
+ Contract.verify(Numeric === b)
17
+
18
+ a * b
19
+ end
20
+ end
21
+ end
22
+ ```
23
+
24
+ ``` ruby
25
+ class Bar
26
+ include Contract
27
+
28
+ def bar(a, b)
29
+ contract do
30
+ verify(Numeric === a)
31
+ verify(Numeric === b)
32
+
33
+ a * b
34
+ end
35
+ end
36
+ end
37
+ ```
38
+
39
+ When the `Contract` module is included, the methods `contract` and
40
+ `verify` become available.
41
+
42
+ ## Installation
43
+
44
+ $ gem install contract
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "contract"
3
+ s.version = "0.0.1"
4
+ s.summary = "Contract helper"
5
+ s.description = "Contract helper"
6
+ s.authors = ["Michel Martens"]
7
+ s.email = ["michel@soveran.com"]
8
+ s.homepage = "http://github.com/soveran/contract"
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+
12
+ s.add_development_dependency "cutest"
13
+ end
@@ -0,0 +1,29 @@
1
+ # Provides a way to define contracts for a given function.
2
+ # The methods `define` and `verify` can be used directly.
3
+ # If the `Contract` module is included in a class, the
4
+ # methods `contract` and `verify` are added.
5
+ module Contract
6
+
7
+ # Returns the value of the block
8
+ # if all verifications pass
9
+ def self.define(&block)
10
+ catch(:contract) do
11
+ yield
12
+ end
13
+ end
14
+
15
+ # Fails unless value is true
16
+ def self.verify(value)
17
+ value || throw(:contract, false)
18
+ end
19
+
20
+ # Shorthand for Contract.define
21
+ def contract(&block)
22
+ Contract.define(&block)
23
+ end
24
+
25
+ # Shorthand for Contract.verify
26
+ def verify(value)
27
+ Contract.verify(value)
28
+ end
29
+ end
@@ -0,0 +1,4 @@
1
+ test:
2
+ cutest ./test/*.rb
3
+
4
+ .PHONY: test
@@ -0,0 +1,35 @@
1
+ require_relative "../lib/contract"
2
+
3
+ class Foo
4
+ def foo(a, b)
5
+ Contract.define do
6
+ Contract.verify(Numeric === a)
7
+ Contract.verify(Numeric === b)
8
+
9
+ a * b
10
+ end
11
+ end
12
+ end
13
+
14
+ class Bar
15
+ include Contract
16
+
17
+ def bar(a, b)
18
+ contract do
19
+ verify(Numeric === a)
20
+ verify(Numeric === b)
21
+
22
+ a * b
23
+ end
24
+ end
25
+ end
26
+
27
+ test "direct" do
28
+ assert_equal 6, Foo.new.foo(2, 3)
29
+ assert_equal false, Foo.new.foo(2, "3")
30
+ end
31
+
32
+ test "included" do
33
+ assert_equal 6, Bar.new.bar(2, 3)
34
+ assert_equal false, Bar.new.bar(2, "3")
35
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contract
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cutest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Contract helper
28
+ email:
29
+ - michel@soveran.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gems
35
+ - LICENSE
36
+ - README.md
37
+ - contract.gemspec
38
+ - lib/contract.rb
39
+ - makefile
40
+ - test/contract_test.rb
41
+ homepage: http://github.com/soveran/contract
42
+ licenses: []
43
+ metadata: {}
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 2.0.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Contract helper
64
+ test_files: []