kleisli-contracts 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +12 -0
- data/kleisli-contracts.gemspec +26 -0
- data/lib/kleisli/contracts/version.rb +5 -0
- data/lib/kleisli/contracts.rb +51 -0
- data/test/kleisli/contracts_test.rb +42 -0
- data/test/test_helper.rb +5 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YWZiNmM4YmRiNjY0MzcyODMxOGU3NjQ4NTU1ZWU5MDZlZTE5ZWMxOA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NjZkNjNhYzBmYzE3NzFlMDM4Yjc0YjY0YTk1YWRjMWZkZWU2NDYxMg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YzBlMGNmMjQxNzIyNzc4MjgyM2QwZGFlMGQyODdiNTY2NGRkZWE1ZjYxM2Mz
|
10
|
+
YTc3OTFlYzQzYjRmZTc2YTI3NTg1ZjhhZWU5ZjlhZjczNGY2NGIyMDBlOTM3
|
11
|
+
ZDE4OTMxZGMzMmU1MTZmYzg0OWQxYWJjZDBhMjRlZmM0MGJhNmU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
OTg2ZWY3MjBmMzA3YzA4YjMyNDQ3YmJjYzhmMDdhZTg0ZjAyM2I4MjJjOTM3
|
14
|
+
OWZiNjBmZTMzYTc5ZTk4ZWMwYmZkOWVmMGMwNTY0MTg0ODUxZjgxNzlhNDg5
|
15
|
+
OWQzMTM2MzRiNDlhYTg5YzEzYWE4NTAwZTQyNDQwZTIwZGFiYzg=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Brian Zeligson
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Kleisli::Contracts
|
2
|
+
|
3
|
+
Provides the following [Contracts](https://github.com/egonSchiele/contracts.ruby)
|
4
|
+
for monads provided by the [Kleisli](https://github.com/txus/kleisli) gem:
|
5
|
+
|
6
|
+
```ruby
|
7
|
+
MaybeOf[SomeType]
|
8
|
+
EitherOf[LeftType, RightType]
|
9
|
+
ValidationOf[FailureType, SuccessType]
|
10
|
+
```
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'kleisli-contracts'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install kleisli-contracts
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Use of Contracts is well documented [here](http://egonschiele.github.io/contracts.ruby/)
|
31
|
+
|
32
|
+
Use of Maybe and Either are well documented [here](http://blog.txus.io/kleisli/)
|
33
|
+
|
34
|
+
Use of Validation is well documented [here](https://github.com/beezee/kleisli-validation)
|
35
|
+
|
36
|
+
Example use can be seen in the [tests](https://github.com/beezee/kleisli-contracts/blob/master/test/kleisli/contracts_test.rb)
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it ( https://github.com/[my-github-username]/kleisli-contracts/fork )
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kleisli/contracts/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "kleisli-contracts"
|
8
|
+
spec.version = Kleisli::Contracts::VERSION
|
9
|
+
spec.authors = ["Brian Zeligson"]
|
10
|
+
spec.email = ["brian.zeligson@gmail.com"]
|
11
|
+
spec.summary = %q{contracts.ruby Contracts for the monads defined in the Kleisli gem}
|
12
|
+
spec.description = %q{}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
spec.add_runtime_dependency "kleisli-validation"
|
25
|
+
spec.add_runtime_dependency "contracts"
|
26
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "kleisli/contracts/version"
|
2
|
+
require "kleisli/validation"
|
3
|
+
require "contracts"
|
4
|
+
|
5
|
+
module Kleisli
|
6
|
+
module Contracts
|
7
|
+
include ::Contracts
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.send(:include, ::Contracts)
|
11
|
+
end
|
12
|
+
|
13
|
+
class KContract < CallableClass
|
14
|
+
def initialize(*inner_types)
|
15
|
+
@inner_types = inner_types
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class MaybeOf < KContract
|
20
|
+
def valid?(val)
|
21
|
+
return false unless val.kind_of?(Kleisli::Maybe) &&
|
22
|
+
@inner_types.size == 1
|
23
|
+
inner_valid, _ = Contract.valid?(val.value, @inner_types.first)
|
24
|
+
inner_valid || val.value.nil?
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class EitherOf < KContract
|
29
|
+
def monad_valid(val)
|
30
|
+
[Kleisli::Either::Left, Kleisli::Either::Right].
|
31
|
+
include?(val.class)
|
32
|
+
end
|
33
|
+
def valid?(val)
|
34
|
+
return false unless monad_valid(val) &&
|
35
|
+
@inner_types.size == 2
|
36
|
+
left_valid, _ = Contract.valid?(val.left, @inner_types.first)
|
37
|
+
right_valid, _ = Contract.valid?(val.right, @inner_types.last)
|
38
|
+
(val.left.nil? && right_valid) ||
|
39
|
+
(val.right.nil? && left_valid)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
class ValidationOf < EitherOf
|
44
|
+
def monad_valid(val)
|
45
|
+
[Kleisli::Validation::Failure, Kleisli::Validation::Success].
|
46
|
+
include?(val.class)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ContractsTest < Minitest::Test
|
4
|
+
include Kleisli::Contracts
|
5
|
+
|
6
|
+
Contract MaybeOf[MaybeOf[Num]] => MaybeOf[Num]
|
7
|
+
def maybe_add(m_int)
|
8
|
+
m_int >-> x { x.fmap { |y| y + 1 } }
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_maybe
|
12
|
+
assert_equal(Some(4), maybe_add(Some(Some(3))))
|
13
|
+
assert_raises(ContractError) { maybe_add(Some(Some("b"))) }
|
14
|
+
end
|
15
|
+
|
16
|
+
Contract EitherOf[String, String] => EitherOf[String, Num]
|
17
|
+
def either_to_i(either_string)
|
18
|
+
either_string >-> x { x.to_i > 0 ? Right(x.to_i) : Left("sorry") }
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_either
|
22
|
+
assert_equal(Right(3), either_to_i(Right("3")))
|
23
|
+
assert_equal(Left("nope"), either_to_i(Left("nope")))
|
24
|
+
assert_equal(Left("sorry"), either_to_i(Right("fif")))
|
25
|
+
assert_raises(ContractError) { either_to_i(Right(3)) }
|
26
|
+
assert_raises(ContractError) { either_to_i(Left(5)) }
|
27
|
+
end
|
28
|
+
|
29
|
+
VNum = ValidationOf[ArrayOf[String], Num]
|
30
|
+
Contract VNum, VNum => VNum
|
31
|
+
def validated_sum(n1, n2)
|
32
|
+
Success(-> x, y { x + y }) * n1 * n2
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_validation
|
36
|
+
assert_equal(Success(3), validated_sum(Success(1), Success(2)))
|
37
|
+
assert_equal(Failure(["nope"]), validated_sum(Success(2), Failure(["nope"])))
|
38
|
+
assert_equal(Failure(["sorry", "nope"]), validated_sum(Failure(["sorry"]), Failure(["nope"])))
|
39
|
+
assert_raises(ContractError) { validated_sum(Success(3), Failure("missing array")) }
|
40
|
+
assert_raises(ContractError) { validated_sum(Success("3"), Success(4)) }
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kleisli-contracts
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Zeligson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: kleisli-validation
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: contracts
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: ''
|
84
|
+
email:
|
85
|
+
- brian.zeligson@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- kleisli-contracts.gemspec
|
96
|
+
- lib/kleisli/contracts.rb
|
97
|
+
- lib/kleisli/contracts/version.rb
|
98
|
+
- test/kleisli/contracts_test.rb
|
99
|
+
- test/test_helper.rb
|
100
|
+
homepage: ''
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ! '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.4.4
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: contracts.ruby Contracts for the monads defined in the Kleisli gem
|
124
|
+
test_files:
|
125
|
+
- test/kleisli/contracts_test.rb
|
126
|
+
- test/test_helper.rb
|