consequence 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5cdcccf4da40e5989a2efb8bb5bf8d74d3a3a24f
4
- data.tar.gz: df4593261d32a4e41a7cbf741b80a59395b1b55d
3
+ metadata.gz: 6cdf2c9dad1c8995f8853db69da3d6804daf2029
4
+ data.tar.gz: bfc2e9093a371c54ee2c39c273c2c5ffea14ba18
5
5
  SHA512:
6
- metadata.gz: 1f964a3833b3393c21f9aa8d4c7be8631885488967b44e4e2ffd832a80f0b24cbfb93e008afeb5a63c19defc9a6f4abdea7859375d558a22e4d7ec60142e3146
7
- data.tar.gz: 899f36b13722699b98b811c48b1b9887744afbebe0f94ea85af59c74c535900c180742fe34cfea70ac1f60206dbf9aa83dbd19ee596207b87dd2259bda9d6c81
6
+ metadata.gz: 3066cbf8aa5a1415c37788e81f550c9ad96cc6b4a69604c26a2bc2ac8936e9ee610edc20b49bb694f37082b8cc8a084c0e9660f9e4bfb0085a00622550c0014b
7
+ data.tar.gz: 845c28a333e1ef7676cba5bbb6af7846eed3af5495e184b64a60f64adc7ebf3e128684886f59122d9bc804874f196154f77664cf5d0a1cc91622c3f56dbb6f71
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Consequence
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/consequence.svg)](http://badge.fury.io/rb/consequence)
4
+
3
5
  Monad implementation to be used with [contracts.ruby](https://github.com/egonSchiele/contracts.ruby)
4
6
 
5
7
  ## Example Usage
@@ -7,14 +9,12 @@ Monad implementation to be used with [contracts.ruby](https://github.com/egonSch
7
9
  ``` ruby
8
10
  require 'consequence'
9
11
 
10
- class LicensePirate
12
+ class HirePirate
11
13
  include Consequence
12
14
  alias_method :m, :method
13
15
 
14
16
  def apply(applicant)
15
- Success[applicant] >> m(:eye_patch_check)
16
- << m(:log)
17
- >> :sign_and_date
17
+ Success[applicant] >> m(:eye_patch_check) << m(:log) >> :sign_and_date
18
18
  end
19
19
 
20
20
  private
@@ -56,7 +56,56 @@ If called with a Symbol instead of a method, it is sent as a message to the valu
56
56
 
57
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
58
 
59
- ### Others to be implemented shortly.
59
+ ### Option
60
+
61
+ A Option Monad only applies a method if it's value is not nil, otherwise it ignores them. This prevents MissingMethod errors from methods trying to be applied to nil.
62
+
63
+ ## Example Implementation of Left and Right
64
+
65
+ ``` ruby
66
+ require 'consequence'
67
+
68
+ class Crab
69
+ include Consequence
70
+ alias_method :m, :method
71
+
72
+ Contract Monad => Monad
73
+ def begin(direction = Left[0])
74
+ direction >> m(:turn) >> m(:move) << m(:log) >> m(:continue?)
75
+ end
76
+
77
+ private
78
+
79
+ Left = Class.new(Monad)
80
+ Right = Class.new(Monad)
81
+
82
+ Contract Left => Num
83
+ def move(monad)
84
+ monad.value + 5
85
+ end
86
+
87
+ Contract Right => Num
88
+ def move(monad)
89
+ monad.value - 5
90
+ end
91
+
92
+ Contract Num => Monad
93
+ def turn(value)
94
+ rand > 0.5 ? Left[value] : Right[value]
95
+ end
96
+
97
+ def log(value)
98
+ puts value
99
+ end
100
+
101
+ Contract Monad => Monad
102
+ def continue?(monad)
103
+ monad.value.abs > 25 ? monad : monad >> m(:begin)
104
+ end
105
+ end
106
+
107
+ Crab.new.begin
108
+ ```
60
109
 
61
110
  ## Installation
62
111
 
data/lib/consequence.rb CHANGED
@@ -9,3 +9,4 @@ end
9
9
  require 'consequence/monad'
10
10
  require 'consequence/failure'
11
11
  require 'consequence/success'
12
+ require 'consequence/option'
@@ -44,6 +44,14 @@ module Consequence
44
44
  self.class == other.class && value == other.value
45
45
  end
46
46
 
47
+ def to_s
48
+ "#{self.class}[#{value}]"
49
+ end
50
+
51
+ def inspect
52
+ to_s
53
+ end
54
+
47
55
  private
48
56
 
49
57
  def bind(proc)
@@ -0,0 +1,13 @@
1
+ module Consequence
2
+ class Option < Monad
3
+ def >>(proc)
4
+ value ? super : self
5
+ end
6
+
7
+ def <<(proc)
8
+ super if value
9
+ end
10
+
11
+ def nil?; value.nil? end
12
+ end
13
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: consequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max White
@@ -61,6 +61,7 @@ files:
61
61
  - lib/consequence.rb
62
62
  - lib/consequence/failure.rb
63
63
  - lib/consequence/monad.rb
64
+ - lib/consequence/option.rb
64
65
  - lib/consequence/success.rb
65
66
  homepage:
66
67
  licenses: