deterministic 0.4.0 → 0.5.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: 16ab904730d38a8b5b0e3b3a2e6a8e40f3bf68fa
4
- data.tar.gz: 29b690c3723bfcce5c2cfe40fcc03db7059aee5f
3
+ metadata.gz: 53f5ad82104a1c379dd88c86c926bfb62ceca2e0
4
+ data.tar.gz: d7efc5de945b130c59a7005d182e263622d46815
5
5
  SHA512:
6
- metadata.gz: 8042eed9f812f6570fbfa18dfdb2f5245d53bbd2419dbc44fdefb7a760d5da575b19064ced2723bc82724fd4866767728951e73e89ccb3a157a1912db1d89690
7
- data.tar.gz: 62e5ec295f1d1535b77cb271def694ecd8d22291e0ed39ad6f708539273cc8dca03ea81af277375f139690b1e912cef4da30f0a251d3063534bec27c9c7ac4cd
6
+ metadata.gz: 9459fad3f2cb626638b2cdbe6838659c9d86306884df71b8a455c31be146f71e40bd5241d65cedd5c132ec1e4b13c090b231964e79ae03409d6a16cde23d3123
7
+ data.tar.gz: 7a04167f5f8856a7026e157cb20c4bbeebb5a669f0e5e8bc236ccc6efb1a7515a9abf0600bb7eef0821df414148b833f17e54dfbeb32c5d80e1a5dc7cdb55e69
data/README.md CHANGED
@@ -8,6 +8,21 @@ This gem is still __WORK IN PROGRESS__.
8
8
 
9
9
  ## Usage
10
10
 
11
+ ### Success & Failure
12
+
13
+ ```ruby
14
+ Success(1).to_s # => "1"
15
+ Success(1) << Success(2) # => Success(2)
16
+ Success(Success(1)) # => Success(1)
17
+ Success(1).map { |v| v + 1} # => Success(2)
18
+ Success({a:1}).to_json # => '{"Success": {"a":1}}'
19
+
20
+ Failure(1).to_s # => "1"
21
+ Failure(1) << Failure(2) # => Failure(1)
22
+ Failure(Failure(1)) # => Failure(1)
23
+ Failure(1).map { |v| v + 1} # => Failure(2)
24
+ Failure({a:1}).to_json # => '{"Failure": {"a":1}}'
25
+
11
26
  ### Either#attempt_all
12
27
  The basic idea is to execute a chain of units of work and make sure all return either `Success` or `Failure`.
13
28
 
@@ -19,6 +19,15 @@ module Deterministic
19
19
  class << self
20
20
  protected :new
21
21
  end
22
+
23
+ def to_json
24
+ name = self.class.name.split('::')[-1]
25
+ "{\"#{name}\":#{value.to_json}}"
26
+ end
27
+
28
+ def to_s
29
+ value.to_s
30
+ end
22
31
  end
23
32
 
24
33
  module_function
@@ -2,7 +2,7 @@ module Deterministic
2
2
  module Monad
3
3
  class NotMonadError < StandardError; end
4
4
 
5
- def initialize(value)
5
+ def initialize(value)
6
6
  @value = join(value)
7
7
  end
8
8
 
@@ -1,3 +1,3 @@
1
1
  module Deterministic
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require_relative '../monad_axioms'
3
+
4
+ include Deterministic
5
+
6
+ describe Deterministic::Failure do
7
+
8
+ it_behaves_like 'a Monad' do
9
+ let(:monad) { described_class }
10
+ end
11
+
12
+ subject { described_class.new(1) }
13
+
14
+ specify { expect(subject).to be_an_instance_of described_class }
15
+ specify { expect(subject.value).to eq 1 }
16
+ specify { expect(subject).to be_failure }
17
+ specify { expect(subject).not_to be_success }
18
+
19
+ specify { expect(subject).to be_an_instance_of described_class }
20
+ specify { expect(subject).to eq(described_class.new(1)) }
21
+ specify { expect(subject << Success(2)).to eq(Failure(1)) }
22
+ specify { expect(subject << Failure(2)).to eq(Failure(1)) }
23
+ specify { expect(Failure(subject)).to eq Failure(1) }
24
+ specify { expect(subject.map { |v| v + 1} ).to eq Failure(2) }
25
+
26
+ it "#to_json" do
27
+ expect(Failure({a: 1}).to_json).to eq '{"Failure":{"a":1}}'
28
+ end
29
+
30
+ it "#to_s" do
31
+ expect(Failure(1).to_s).to eq "1"
32
+ expect(Failure({a: 1}).to_s).to eq "{:a=>1}"
33
+ end
34
+ end
@@ -5,7 +5,7 @@ include Deterministic
5
5
 
6
6
  describe Deterministic::Success do
7
7
 
8
- it_behaves_like 'a Monad' do
8
+ it_behaves_like 'a Monad' do
9
9
  let(:monad) { described_class }
10
10
  end
11
11
 
@@ -28,4 +28,13 @@ describe Deterministic::Success do
28
28
  subject.bind { |v| true ? Success(v + 1) : Failure(v + 2)}
29
29
  ).to eq Success(2)
30
30
  end
31
+
32
+ it "#to_json" do
33
+ expect(Success({a: 1}).to_json).to eq '{"Success":{"a":1}}'
34
+ end
35
+
36
+ it "#to_s" do
37
+ expect(Success(1).to_s).to eq "1"
38
+ expect(Success({a: 1}).to_s).to eq "{:a=>1}"
39
+ end
31
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deterministic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Zolnierek
@@ -140,6 +140,7 @@ files:
140
140
  - spec/lib/deterministic/attempt_all_spec.rb
141
141
  - spec/lib/deterministic/core_ext/either_spec.rb
142
142
  - spec/lib/deterministic/core_ext/object/either_spec.rb
143
+ - spec/lib/deterministic/either/failure_spec.rb
143
144
  - spec/lib/deterministic/either/match_spec.rb
144
145
  - spec/lib/deterministic/either/success_spec.rb
145
146
  - spec/lib/deterministic/either_spec.rb
@@ -177,6 +178,7 @@ test_files:
177
178
  - spec/lib/deterministic/attempt_all_spec.rb
178
179
  - spec/lib/deterministic/core_ext/either_spec.rb
179
180
  - spec/lib/deterministic/core_ext/object/either_spec.rb
181
+ - spec/lib/deterministic/either/failure_spec.rb
180
182
  - spec/lib/deterministic/either/match_spec.rb
181
183
  - spec/lib/deterministic/either/success_spec.rb
182
184
  - spec/lib/deterministic/either_spec.rb