consequence 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5cdcccf4da40e5989a2efb8bb5bf8d74d3a3a24f
4
+ data.tar.gz: df4593261d32a4e41a7cbf741b80a59395b1b55d
5
+ SHA512:
6
+ metadata.gz: 1f964a3833b3393c21f9aa8d4c7be8631885488967b44e4e2ffd832a80f0b24cbfb93e008afeb5a63c19defc9a6f4abdea7859375d558a22e4d7ec60142e3146
7
+ data.tar.gz: 899f36b13722699b98b811c48b1b9887744afbebe0f94ea85af59c74c535900c180742fe34cfea70ac1f60206dbf9aa83dbd19ee596207b87dd2259bda9d6c81
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Max White
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.
@@ -0,0 +1,75 @@
1
+ # Consequence
2
+
3
+ Monad implementation to be used with [contracts.ruby](https://github.com/egonSchiele/contracts.ruby)
4
+
5
+ ## Example Usage
6
+
7
+ ``` ruby
8
+ require 'consequence'
9
+
10
+ class LicensePirate
11
+ include Consequence
12
+ alias_method :m, :method
13
+
14
+ def apply(applicant)
15
+ Success[applicant] >> m(:eye_patch_check)
16
+ << m(:log)
17
+ >> :sign_and_date
18
+ end
19
+
20
+ private
21
+
22
+ Contract Applicant => Monad
23
+ def eye_patch_check(applicant)
24
+ applicant.working_eyes > 1 ? Failure['Too many eyes'] : Success[Pirate.new(applicant)]
25
+ end
26
+
27
+ def log(pirate)
28
+ puts "#{pirate.name} has joined the crew"
29
+ end
30
+ end
31
+ ```
32
+
33
+ ## Monad Methods
34
+
35
+ ### >>
36
+
37
+ Chains a method with the result being passed down the chain.
38
+
39
+ If the method has a contract that requires a Monad, then the method is passed the Monad, otherwise it is passed it's value.
40
+
41
+ If the method has a contract that returns a Monad, then that Monad is passed down the chain, otherwise the result of the method is taken as the value for a new Monad.
42
+
43
+ If called with a Symbol instead of a method, it is sent as a message to the value, and the result is taken as the value for a new Monad.
44
+
45
+ ### <<
46
+
47
+ Method is applied with the result being ignored and the unchanged Monad is passed down the chain.
48
+
49
+ If the method has a contract that requires a Monad, then the method is passed the Monad, otherwise it is passed it's value.
50
+
51
+ If called with a Symbol instead of a method, it is sent as a message to the value.
52
+
53
+ ## Monad Types
54
+
55
+ ### Success & Failure
56
+
57
+ A Success Monad wraps up all exceptions in a Failed Monad and a Failed Monad ignores all chained methods. This allows all possible failures in a long process to be dealt with at the end.
58
+
59
+ ### Others to be implemented shortly.
60
+
61
+ ## Installation
62
+
63
+ Add this line to your application's Gemfile:
64
+
65
+ ```ruby
66
+ gem 'consequence'
67
+ ```
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( https://github.com/[my-github-username]/consequence/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
74
+ 4. Push to the branch (`git push origin my-new-feature`)
75
+ 5. Create a new Pull Request
@@ -0,0 +1,11 @@
1
+ require 'contracts'
2
+
3
+ module Consequence
4
+ def self.included(base)
5
+ base.include(Contracts)
6
+ end
7
+ end
8
+
9
+ require 'consequence/monad'
10
+ require 'consequence/failure'
11
+ require 'consequence/success'
@@ -0,0 +1,8 @@
1
+ module Consequence
2
+ class Failure < Monad
3
+ def >>(_); self end
4
+ def <<(_); self end
5
+ def succeeded?; false end
6
+ def failed?; true end
7
+ end
8
+ end
@@ -0,0 +1,61 @@
1
+ module Consequence
2
+ class Monad
3
+ include Contracts
4
+
5
+ def self.[](value)
6
+ new(value)
7
+ end
8
+
9
+ def initialize(value)
10
+ @value = value
11
+ end
12
+
13
+ attr_reader :value
14
+
15
+ # >>
16
+ #########
17
+ Contract Symbol => Monad
18
+ define_method(:>>) { |symbol| wrap(vbind(symbol.to_proc)) }
19
+
20
+ Contract Func[Not[Monad] => Not[Monad]] => Monad
21
+ define_method(:>>) { |proc| wrap(vbind(proc)) }
22
+
23
+ Contract Func[Not[Monad] => Monad] => Monad
24
+ define_method(:>>) { |proc| vbind(proc) }
25
+
26
+ Contract Func[Monad => Monad] => Monad
27
+ define_method(:>>) { |proc| bind(proc) }
28
+
29
+ Contract Func[Monad => Not[Monad]] => Monad
30
+ define_method(:>>) { |proc| wrap(bind(proc)) }
31
+
32
+ # <<
33
+ #########
34
+ Contract Symbol => Monad
35
+ define_method(:<<) { |symbol| vbind(symbol.to_proc); self }
36
+
37
+ Contract Func[Not[Monad] => Any] => Monad
38
+ define_method(:<<) { |proc| vbind(proc); self }
39
+
40
+ Contract Func[Monad => Any] => Monad
41
+ define_method(:<<) { |proc| bind(proc); self }
42
+
43
+ def ==(other)
44
+ self.class == other.class && value == other.value
45
+ end
46
+
47
+ private
48
+
49
+ def bind(proc)
50
+ proc.call(self)
51
+ end
52
+
53
+ def vbind(proc)
54
+ proc.call(value)
55
+ end
56
+
57
+ def wrap(value)
58
+ self.class[value]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,18 @@
1
+ module Consequence
2
+ class Success < Monad
3
+ def >>(proc)
4
+ super
5
+ rescue => err
6
+ Failure[err]
7
+ end
8
+
9
+ def <<(proc)
10
+ super
11
+ rescue => err
12
+ Failure[err]
13
+ end
14
+
15
+ def succeeded?; true end
16
+ def failed?; false end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: consequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: contracts
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 0.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '0.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.4.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.1'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 3.1.0
43
+ type: :development
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '3.1'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 3.1.0
53
+ description:
54
+ email: mushishi78@gmail.com
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - LICENSE.txt
60
+ - README.md
61
+ - lib/consequence.rb
62
+ - lib/consequence/failure.rb
63
+ - lib/consequence/monad.rb
64
+ - lib/consequence/success.rb
65
+ homepage:
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Monad implementation to used in with contracts.ruby
89
+ test_files: []