unsound 0.1.2 → 0.1.3

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: 969cc5da6572e62fb76773536cd7bc3da5a0df11
4
- data.tar.gz: 32ed2346c15304d27addb2b0d623ca68738f9f84
3
+ metadata.gz: 8032f0925e0db77e85eaa9d2f3fe0b052ab73a1f
4
+ data.tar.gz: 90a75d957569e6cb264240b7f28f561cc17e56cb
5
5
  SHA512:
6
- metadata.gz: ee5567a88fe014b0c6afa9652d71a52e00575017206fd4319d0c8bf018f87b646cddc8aa3ade4e29cad9e612743aa8322caf13a62376c8e3524735090fbb03ef
7
- data.tar.gz: 3f0ac46526183250bbe08f3bcaf8946f9dc6eff7760265c9cdb1cc10e0c0f9f67f149b006bfa7a539e497b197bf7ed0315bcfacf0f0706aec395d6822e9ea440
6
+ metadata.gz: 6cfe4311ce77b5fa378febd2156965a2aaa23416fe010c21d89a1eb53b16d74bda4ed2a51ba63983e33e348478f7eac8842fa3b155a5deb20d2fc7226de0c937
7
+ data.tar.gz: af4704334655a2adc6dd688e6412f0d97c1d43f230c0d8a00d588f3d9a1fa49632ddf87b0d5f0bc9ab8e8f23d8da2b36ac36b5ca540075361652fc2ad5e4a7b6
@@ -17,6 +17,15 @@ module Unsound
17
17
  Data::Left.new($ERROR_INFO)
18
18
  end
19
19
 
20
+
21
+ # Like .try, but wraps the block in a Proc for lazy execution
22
+ # @param block [Block] the block to execute
23
+ # @return [Proc] a proc which will execute the supplied block using .try
24
+ # when executed.
25
+ def wrap_with_try(&block)
26
+ ->(*args) { try { block.call(*args) } }
27
+ end
28
+
20
29
  # Execute the block. If the block
21
30
  # results in Nil, return {Data::Nothing},
22
31
  # otherwise wrap the result in a {Data::Just}
@@ -30,5 +39,13 @@ module Unsound
30
39
  Data::Just.new(result)
31
40
  end
32
41
  end
42
+
43
+ # Like .maybe, but wraps the block in a Proc for lazy execution
44
+ # @param block [Block] the block to execute
45
+ # @return [Proc] a proc which will execute the supplied block using .maybe
46
+ # when executed.
47
+ def wrap_with_maybe(&block)
48
+ ->(*args) { maybe{ block.call(*args) } }
49
+ end
33
50
  end
34
51
  end
@@ -1,3 +1,3 @@
1
1
  module Unsound
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -25,6 +25,32 @@ RSpec.describe Unsound::Control do
25
25
  end
26
26
  end
27
27
 
28
+ describe Unsound::Control, ".wrap_with_try" do
29
+ subject(:wrapped_try) { Unsound::Control.wrap_with_try(&blk) }
30
+
31
+ context "the block executes successfully" do
32
+ let(:blk) { ->(argument) { result } }
33
+ let(:result) { double(:result) }
34
+
35
+ it "returns the result wrapped in a Right when it is called" do
36
+ result_of_call = wrapped_try.call("foo")
37
+ expect(result_of_call).to be_a(Unsound::Data::Right)
38
+ expect(result_of_call.value).to eq(result)
39
+ end
40
+ end
41
+
42
+ context "the block raises an exception" do
43
+ let(:blk) { ->(argument) { fail error } }
44
+ let(:error) { StandardError.new("Something went wrong") }
45
+
46
+ it "returns the exception wrapped in a Left when it is called" do
47
+ result_of_call = wrapped_try.call("foo")
48
+ expect(result_of_call).to be_a(Unsound::Data::Left)
49
+ expect(result_of_call.value).to eq(error)
50
+ end
51
+ end
52
+ end
53
+
28
54
  describe Unsound::Control, ".maybe" do
29
55
  subject(:run_maybe) { Unsound::Control.maybe(&blk) }
30
56
 
@@ -46,4 +72,28 @@ RSpec.describe Unsound::Control do
46
72
  end
47
73
  end
48
74
  end
75
+
76
+ describe Unsound::Control, ".wrap_with_maybe" do
77
+ subject(:wrapped_maybe) { Unsound::Control.wrap_with_maybe(&blk) }
78
+
79
+ context "the block returns nil" do
80
+ let(:blk) { ->(argument) { nil } }
81
+
82
+ it "returns Nothing when it is called" do
83
+ result_of_call = wrapped_maybe.call("foo")
84
+ expect(result_of_call).to be_a(Unsound::Data::Nothing)
85
+ end
86
+ end
87
+
88
+ context "the block does not return nil" do
89
+ let(:blk) { ->(argument) { result } }
90
+ let(:result) { double(:result) }
91
+
92
+ it "returns the result wrapped in a Just when it is called" do
93
+ result_of_call = wrapped_maybe.call("foo")
94
+ expect(result_of_call).to be_a(Unsound::Data::Just)
95
+ expect(result_of_call.value).to eq(result)
96
+ end
97
+ end
98
+ end
49
99
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unsound
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Swan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-28 00:00:00.000000000 Z
11
+ date: 2016-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concord