consequence 0.2.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +13 -15
- data/lib/consequence/monad.rb +14 -2
- data/lib/consequence/success.rb +1 -7
- metadata +21 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1079183708dfee59ddbd8608c469d144d01485e3
|
4
|
+
data.tar.gz: 7e074ed7c93168087ba87c4f1c23610cbf96988b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d713f87a271324cf60063278a996ffc4ff755f7f8241f38fcd21bdd5a50313e72f39f7f857cd6fc0dd0b315136b4b02ef60a08bbd35fa70ad647ff94213caef
|
7
|
+
data.tar.gz: 0fa47ea9c831df1b0b104c6076f11b790229b08932287b3e57fb8dae17826123fd744892d334503393b88b9a675e06f486a7eaa8f11a90a96f21a0ee5e773f8a
|
data/README.md
CHANGED
@@ -17,20 +17,20 @@ include Consequence
|
|
17
17
|
my_monad = Monad[4]
|
18
18
|
```
|
19
19
|
|
20
|
-
Its value can be retrieved with the `#value` getter method
|
20
|
+
Its value can be retrieved with the `#value` getter method:
|
21
21
|
|
22
22
|
``` ruby
|
23
23
|
my_monad.value # 4
|
24
24
|
```
|
25
25
|
|
26
|
-
To create new monad types, simply inherit from `Monad
|
26
|
+
To create new monad types, simply inherit from `Monad`:
|
27
27
|
|
28
28
|
``` ruby
|
29
|
-
Foo
|
30
|
-
Bar
|
29
|
+
class Foo < Monad; end
|
30
|
+
class Bar < Monad; end
|
31
31
|
```
|
32
32
|
|
33
|
-
A monad is equal to another monad only if both it's type and value are equal
|
33
|
+
A monad is equal to another monad only if both it's type and value are equal:
|
34
34
|
|
35
35
|
``` ruby
|
36
36
|
Foo[0] == Foo[1] # false
|
@@ -38,6 +38,13 @@ Foo[0] == Bar[0] # false
|
|
38
38
|
Foo[0] == Foo[0] # true
|
39
39
|
```
|
40
40
|
|
41
|
+
A monad respond to query methods for all defined monads:
|
42
|
+
|
43
|
+
``` ruby
|
44
|
+
Foo[0].foo? # true
|
45
|
+
Foo[0].bar? # false
|
46
|
+
```
|
47
|
+
|
41
48
|
### Operations
|
42
49
|
|
43
50
|
Monads have two main operations `>>` and `<<`.
|
@@ -54,7 +61,7 @@ Foo[4] << ->(v) { puts v }
|
|
54
61
|
It's second argument will be passed the monad itself. This is useful for making decisions based on the monads type.
|
55
62
|
|
56
63
|
``` ruby
|
57
|
-
Foo[0] << ->(v, m) { puts v if m.
|
64
|
+
Foo[0] << ->(v, m) { puts v if m.foo? }
|
58
65
|
# $ 0
|
59
66
|
```
|
60
67
|
|
@@ -136,15 +143,6 @@ Success[0] >> ->(v) { 5 / v }
|
|
136
143
|
|
137
144
|
A `Failure` monad is a subclass of the `NullMonad` so all successive chained operations are ignored.
|
138
145
|
|
139
|
-
Both `Success` and `Failure` respond to the `#succeeded?` and `#failed?` query methods in the way you'd expect:
|
140
|
-
|
141
|
-
``` ruby
|
142
|
-
Success[0].succeeded? # true
|
143
|
-
Success[0].failed? # false
|
144
|
-
Failure[0].succeeded? # false
|
145
|
-
Failure[0].failed? # true
|
146
|
-
```
|
147
|
-
|
148
146
|
For an example, check out the [Success & Failure example](https://github.com/mushishi78/consequence/wiki/Success-&-Failure-Example) on the wiki.
|
149
147
|
|
150
148
|
### Something & Nothing
|
data/lib/consequence/monad.rb
CHANGED
@@ -1,7 +1,19 @@
|
|
1
|
+
require 'inflecto'
|
2
|
+
|
1
3
|
module Consequence
|
2
4
|
class Monad
|
3
|
-
|
4
|
-
|
5
|
+
class << self
|
6
|
+
def [](value)
|
7
|
+
new(value)
|
8
|
+
end
|
9
|
+
|
10
|
+
def inherited(child)
|
11
|
+
return unless child.name
|
12
|
+
query_name = Inflecto.demodulize(child.name)
|
13
|
+
query_name = Inflecto.underscore(query_name)
|
14
|
+
query_name = "#{query_name}?".to_sym
|
15
|
+
define_method(query_name) { self.is_a?(child) }
|
16
|
+
end
|
5
17
|
end
|
6
18
|
|
7
19
|
def initialize(value)
|
data/lib/consequence/success.rb
CHANGED
@@ -13,13 +13,7 @@ module Consequence
|
|
13
13
|
rescue => err
|
14
14
|
Failure[err]
|
15
15
|
end
|
16
|
-
|
17
|
-
def succeeded?; true end
|
18
|
-
def failed?; false end
|
19
16
|
end
|
20
17
|
|
21
|
-
class Failure < NullMonad
|
22
|
-
def succeeded?; false end
|
23
|
-
def failed?; true end
|
24
|
-
end
|
18
|
+
class Failure < NullMonad; end
|
25
19
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max White
|
@@ -10,6 +10,26 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: inflecto
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.0.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.0.2
|
13
33
|
- !ruby/object:Gem::Dependency
|
14
34
|
name: rspec
|
15
35
|
requirement: !ruby/object:Gem::Requirement
|