kleisli-conversions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDcxNmI4ZGUwMWQ2NGE0MGI5N2YyMGNhZDQ5NjhmYjUxYjcxZThiZA==
5
+ data.tar.gz: !binary |-
6
+ MjVlYzcxZGE2NTE5ZDgzMjBmYTc4NjE4YjE0NWYyNzI0MzIxNDdiOQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NGFkNDc4YTVlMTFlZGUxODZiNWYwMGI1NDI1NjRlNTllZWE0NjIwYmVkZWZi
10
+ ODNjZjg0OTZjYTc0YThkZDJjMGQxOGUxZWE1Yzk3NjMwNDA2ODQxNjc0NjY2
11
+ YWY3NjExYmFjZDgzMDIyMTNlNWE5MzlmODY3OWIyMmVjOGU0MDY=
12
+ data.tar.gz: !binary |-
13
+ ZjczOGYyNzcxM2Q5ODA1NWNlMjc5MmI4MjU2ZmE4ZmNlNzg3MTk0YjcwMmRh
14
+ YmUzZDcyODU0NjZlOTRkZjYzMzc3NjZiNWJkNjhjMGQwYmVmNzNlNGJhZmNj
15
+ OTkxMzk5NTcwNWRhYTFhNjBjNWExMGRmYWJjNDMyZWFkMDMzZGI=
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in kleisli-conversions.gemspec
4
+ gemspec
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,95 @@
1
+ # Kleisli::Conversions
2
+
3
+ Adds conversion and lift methods for monads provided by the
4
+ [Kleisli](https://github.com/txus/kleisli) and
5
+ [Kleisli Validation](https://github.com/beezee/kleisli-validation) gems.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'kleisli-conversions'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install kleisli-conversions
22
+
23
+ ## Usage
24
+
25
+ ###Conversions
26
+
27
+ The following conversions are provided by requiring kleisli/conversions:
28
+
29
+ ####Maybe#to_success(msg)
30
+
31
+ Converts Some(value) to Success(value), and None to Failure(msg)
32
+
33
+ ####Either#to_validation
34
+
35
+ Converts Right(value) to Success(value), and Left(value) to Failure(value)
36
+
37
+ ####Try#to_validation
38
+
39
+ Converts Try::Success(value) to Success(value),
40
+ and Try::Failure(value) to Failure(value)
41
+
42
+ ####Validation#fail_hash(key)
43
+
44
+ Converts Failure(value) to Failure(key => value) and noop on Success.
45
+ Useful for accumulating failures with applicative syntax
46
+
47
+ ####Validation#fail_array
48
+
49
+ Converts Failure(value) to Failure([value]) and noop on Success
50
+ Useful for accumulating failures with applicative syntax
51
+
52
+ ###Lift methods
53
+
54
+ The following lift methods are added to any class by calling the following
55
+
56
+ ```ruby
57
+ Kleisli::Conversions::Lift.enrich(MyClass)
58
+ # Kleisli::Conversions::Lift.enrich(Object) to add to everything
59
+ ```
60
+
61
+ ####\#maybe
62
+
63
+ Converts nil to None and non-nil value to Some(value)
64
+
65
+ ####\#some
66
+
67
+ Converts any value to Some(value)
68
+
69
+ ####\#none
70
+
71
+ Converts any value to None
72
+
73
+ ####\#success
74
+
75
+ Converts any value to Success(value)
76
+
77
+ ####\#failure
78
+
79
+ Converts any value to Failure(value)
80
+
81
+ ####\#right
82
+
83
+ Converts any value to Right(value)
84
+
85
+ ####\#left
86
+
87
+ Converts any value to Left(value)
88
+
89
+ ## Contributing
90
+
91
+ 1. Fork it ( https://github.com/[my-github-username]/kleisli-conversions/fork )
92
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
93
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
94
+ 4. Push to the branch (`git push origin my-new-feature`)
95
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
11
+
12
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'kleisli/conversions/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kleisli-conversions"
8
+ spec.version = Kleisli::Conversions::VERSION
9
+ spec.authors = ["Brian Zeligson"]
10
+ spec.email = ["brian.zeligson@gmail.com"]
11
+ spec.summary = %q{Lift and conversion methods for monads provided by 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
+ end
@@ -0,0 +1,111 @@
1
+ require "kleisli/conversions/version"
2
+ require "kleisli/validation"
3
+
4
+ module Kleisli
5
+
6
+ module Conversions
7
+
8
+ module Lift
9
+ def self.enrich(klass)
10
+ klass.send(:include, Kleisli::Conversions::Lift)
11
+ end
12
+
13
+ def maybe
14
+ Maybe(self)
15
+ end
16
+
17
+ def some
18
+ # explicit request for some should support Some(nil)
19
+ Kleisli::Maybe::Some.new(self)
20
+ end
21
+
22
+ def none
23
+ None()
24
+ end
25
+
26
+ def success
27
+ Success(self)
28
+ end
29
+
30
+ def failure
31
+ Failure(self)
32
+ end
33
+
34
+ def left
35
+ Left(self)
36
+ end
37
+
38
+ def right
39
+ Right(self)
40
+ end
41
+ end
42
+
43
+ module ToValidation
44
+
45
+ GetRight = -> x { x.right }
46
+ GetLeft = -> x { x.left }
47
+ GetVal = -> x { x.value }
48
+ GetEx = -> x { x.exception.message }
49
+
50
+ def self.enrich(o_self, sc, fc, sp , fp)
51
+ o_self.send(:define_method, :to_validation) do
52
+ case
53
+ when is_a?(sc)
54
+ Success(sp.call(self))
55
+ when is_a?(fc)
56
+ Failure(fp.call(self))
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ class Validation
64
+
65
+ class Success
66
+ def fail_array
67
+ self
68
+ end
69
+
70
+ def fail_hash(_)
71
+ self
72
+ end
73
+ end
74
+
75
+ class Failure
76
+ def fail_array
77
+ Failure([left])
78
+ end
79
+
80
+ def fail_hash(key)
81
+ Failure(key => left)
82
+ end
83
+ end
84
+ end
85
+
86
+ class Maybe
87
+
88
+ def to_success(msg)
89
+ case
90
+ when is_a?(Some)
91
+ Success(value)
92
+ when is_a?(None)
93
+ Failure(msg)
94
+ end
95
+ end
96
+ end
97
+
98
+ class Either
99
+ Conversions::ToValidation.
100
+ enrich(self, Right, Left,
101
+ Conversions::ToValidation::GetRight,
102
+ Conversions::ToValidation::GetLeft)
103
+ end
104
+
105
+ class Try
106
+ Conversions::ToValidation.
107
+ enrich(self, Success, Failure,
108
+ Conversions::ToValidation::GetVal,
109
+ Conversions::ToValidation::GetEx)
110
+ end
111
+ end
@@ -0,0 +1,5 @@
1
+ module Kleisli
2
+ module Conversions
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ class ConversionsTest < Minitest::Test
4
+ include Kleisli::Conversions
5
+ Kleisli::Conversions::Lift.enrich(Object)
6
+
7
+ def test_to_success
8
+ assert_equal(Success(1), 1.some.to_success('nope'))
9
+ assert_equal(Failure('nope'), 1.none.to_success('nope'))
10
+ end
11
+
12
+ def test_to_validation
13
+ assert_equal(Success(1), 1.right.to_validation)
14
+ assert_equal(Failure(1), 1.left.to_validation)
15
+ assert_equal(Success(1), Try { 1 }.to_validation)
16
+ assert_equal(Failure('nope'), Try { raise 'nope' }.to_validation)
17
+ end
18
+
19
+ def test_fail_hash
20
+ assert_equal(Failure(a: 1), 1.failure.fail_hash(:a))
21
+ assert_equal(Success(1), 1.success.fail_hash(:a))
22
+ end
23
+
24
+ def test_fail_array
25
+ assert_equal(Failure([1]), 1.failure.fail_array)
26
+ assert_equal(Success(1), 1.success.fail_array)
27
+ end
28
+
29
+ def test_lift
30
+ assert_equal(Some(1), 1.maybe)
31
+ assert_equal(None(), nil.maybe)
32
+ assert_equal(Some(nil), nil.some)
33
+ assert_equal(Some(1), 1.some)
34
+ assert_equal(None(), 1.none)
35
+ assert_equal(Success(1), 1.success)
36
+ assert_equal(Failure(1), 1.failure)
37
+ assert_equal(Right(1), 1.right)
38
+ assert_equal(Left(1), 1.left)
39
+ end
40
+ end
@@ -0,0 +1,4 @@
1
+ require 'kleisli/validation'
2
+ require 'kleisli/conversions'
3
+ require 'minitest'
4
+ require 'minitest/autorun'
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kleisli-conversions
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-08 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
+ description: ''
70
+ email:
71
+ - brian.zeligson@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - kleisli-conversions.gemspec
82
+ - lib/kleisli/conversions.rb
83
+ - lib/kleisli/conversions/version.rb
84
+ - test/kleisli/conversions_test.rb
85
+ - test/test_helper.rb
86
+ homepage: ''
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 2.4.4
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Lift and conversion methods for monads provided by the Kleisli gem
110
+ test_files:
111
+ - test/kleisli/conversions_test.rb
112
+ - test/test_helper.rb