consequence 0.2.0 → 0.2.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: 2213b692e96e63757d2f58234f110e448be7a1ef
4
- data.tar.gz: 38ddbf1267f4e1d68d5d3e87e1967a7aeef4ff9d
3
+ metadata.gz: 35e897cf0811f0e794d208bfcff9c243f86fd167
4
+ data.tar.gz: 4d71d729915727771f5ae6e9263bbf1a72fc0093
5
5
  SHA512:
6
- metadata.gz: da489d84fb8b756fa681a34f9fcade73d7af4b1aace229b95ad627d1682484ebdfd52d95948df7ccec6356273c294c03712718cd019320200f19fa477bb16f63
7
- data.tar.gz: 3a3d53e75a35fb4baf47acf9723713edb91f9618a4d6664e0408b2e9065ee191e1c76fd5c01f12c13aa4018e09f0b65b9636d35ba64df5f118ef7df607b17867
6
+ metadata.gz: 434bcae89a6fad08652e5d697a6f9a3ea3e2eadcf6393a68827b087680c1cfa7d49e14546373b45a3380dd1268d5383e647bfa196ddbd38bc1917c1130ef50bf
7
+ data.tar.gz: efd3cb4d3e8bd01b2d2fb99aae2a1ce69257baa7d7da55edb37428e58560b69d1860f2438bfe8cb7229321c62bbc2161da3714d1f2675f003872c743ef1c6980
data/README.md CHANGED
@@ -184,6 +184,12 @@ A `Nothing` responds positively to the `#nil?` method:
184
184
  Nothing[nil].nil? # true
185
185
  ```
186
186
 
187
+ ### Eventually
188
+
189
+ The `Eventually` monad hooks up the callbacks of a chain of methods that may be executed some time in the future. This is useful for dealing with asynochronous calls.
190
+
191
+ For an example, check out the [Eventually example](https://github.com/mushishi78/consequence/wiki/Eventually-Example) on the wiki.
192
+
187
193
  ## DelegatesToValue
188
194
 
189
195
  `DelegatesToValue` is a module that can be included into Monad that adds a `#method_missing` method to delegate calls to its value:
@@ -196,9 +202,6 @@ Foo[[1, 4, 6]].map {|n| n + 1} # Foo[[2, 5, 7]]
196
202
  It delegates via the `>>` operation, so subclasses of the NullMonad will respond to delegated method calls, but still take no action:
197
203
 
198
204
  ``` ruby
199
- Something.include DelegatesToValue
200
- Nothing.include DelegatesToValue
201
-
202
205
  dangrous_hash = {user: {orders: {1 => {price: 3.99} } } }
203
206
 
204
207
  Something[dangrous_hash][:user][:orders][1][:price] # Consequence::Something[3.99]
@@ -207,12 +210,13 @@ Something[dangrous_hash][:user][:orders][2][:price] # Consequence::Nothing[nil]
207
210
 
208
211
  ## Wiki
209
212
 
210
- To find some examples and information about utils, [check out the wiki](https://github.com/mushishi78/consequence/wiki/Consequence) and feel free to contribute to it.
213
+ To find some examples and information about utils, [check out the wiki](https://github.com/mushishi78/consequence/wiki) and feel free to contribute to it.
211
214
 
212
215
  ## Inspirations
213
216
 
214
217
  * [Deterministic](https://github.com/pzol/deterministic)
215
218
  * [Kleisli](https://github.com/txus/kleisli)
219
+ * [Refactoring Ruby with Monads](https://www.youtube.com/watch?v=J1jYlPtkrqQ&feature=youtu.be&a)
216
220
 
217
221
  ## Installation
218
222
 
@@ -1,5 +1,3 @@
1
- require 'consequence/monad'
2
-
3
1
  module Consequence
4
2
  module DelegatesToValue
5
3
  def method_missing(method_name, *args, &b)
@@ -0,0 +1,17 @@
1
+ require 'consequence/monad'
2
+
3
+ module Consequence
4
+ class Eventually < Monad
5
+ def execute(proc)
6
+ value.(proc)
7
+ end
8
+
9
+ def <<(_); self end
10
+
11
+ private
12
+
13
+ def bind(proc)
14
+ ->(callback) { execute(proc.(callback)) }
15
+ end
16
+ end
17
+ end
@@ -1,7 +1,10 @@
1
1
  require 'consequence/monad'
2
+ require 'consequence/delegates_to_value'
2
3
 
3
4
  module Consequence
4
5
  class Something < Monad
6
+ include DelegatesToValue
7
+
5
8
  def >>(proc)
6
9
  result = super
7
10
  result.value.nil? ? Nothing[nil] : result
@@ -9,6 +12,8 @@ module Consequence
9
12
  end
10
13
 
11
14
  class Nothing < NullMonad
15
+ include DelegatesToValue
16
+
12
17
  def nil?; true end
13
18
  end
14
19
  end
@@ -1,6 +1,6 @@
1
1
  module Consequence
2
2
  module Utils
3
- alias_method :m, :method
3
+ alias_method :m, :method
4
4
 
5
5
  def send_to_value(*args)
6
6
  ->(v) { v.send(*args) }
data/lib/consequence.rb CHANGED
@@ -4,5 +4,6 @@ end
4
4
  require 'consequence/monad'
5
5
  require 'consequence/success'
6
6
  require 'consequence/something'
7
+ require 'consequence/eventually'
7
8
  require 'consequence/delegates_to_value'
8
9
  require 'consequence/utils'
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.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Max White
@@ -41,6 +41,7 @@ files:
41
41
  - lib/consequence.rb
42
42
  - lib/consequence/core_ext/utils.rb
43
43
  - lib/consequence/delegates_to_value.rb
44
+ - lib/consequence/eventually.rb
44
45
  - lib/consequence/monad.rb
45
46
  - lib/consequence/something.rb
46
47
  - lib/consequence/success.rb