consequence 0.2.3 → 0.3.0

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: 1d9f0c8c6460d68c6920b23b9d54b164d5e89d6e
4
- data.tar.gz: 9b94446f09dbf982fb67043adc34dff1646b5043
3
+ metadata.gz: 1079183708dfee59ddbd8608c469d144d01485e3
4
+ data.tar.gz: 7e074ed7c93168087ba87c4f1c23610cbf96988b
5
5
  SHA512:
6
- metadata.gz: 52332a52917ef7cc1359d80e9a667cdaff60593a393d035d0f2091f03eb4ec01ec914ac68923bee4f687412349122aff6c7b6af02bc6fad5dde886fc29889dc6
7
- data.tar.gz: 879ea1c9cdebb84d14e8f95e303f7f76bfcd6188fc820a6355092e5a396cddaa7da6dbd1761191d76b78f9f70293cb3b5e3b1329de346ac45ceac199ff8fae8e
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 = Class.new(Monad)
30
- Bar = Class.new(Monad)
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.is_a?(Foo) }
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
@@ -1,7 +1,19 @@
1
+ require 'inflecto'
2
+
1
3
  module Consequence
2
4
  class Monad
3
- def self.[](value)
4
- new(value)
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)
@@ -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.2.3
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