mangrove 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 9289ae193acf40ac02f9fd2c1abfaa21e9a2eaa99367ba37f12508fe86b37b7a
4
- data.tar.gz: 21c2c5271f29b8b93bb7f9658c03c6edfb213d12eb02a8b06b0e520e75ab825a
3
+ metadata.gz: 06e20e8a9945c52ddba260f01fa12a7f8e65fda55fe2a369f85eb296b428eeb7
4
+ data.tar.gz: 524eba3f2d942063c597568e7d432f18cff9f6e5b85efc5eeadb3d055eaaac46
5
5
  SHA512:
6
- metadata.gz: 7bfa222f9fa0e262315f6cd30fd0e1798222450abc14fbb5aa442d2027c58e19e906a37a88f4f2018359c979272d61f05741327beb7f36b216047176ade02ba3
7
- data.tar.gz: 7be4b82b310e40005f171ee72f2513fa70e4e19038513814c7d0195c0c50aa51e720b5a82ec1dd15b8066197461c5f93b0e8fe16d87c572cb37b9c9b007ee545
6
+ metadata.gz: 88cbb663b866dfd8fb559adc76b847a8501e1d96c237e0ef919c339381f142f4ce7a91482770bd239e2f68588793171c900e9b87a57f50f0c290d9982f944b2c
7
+ data.tar.gz: 9c1b5d530382de4eeeeb2cf20e0a18abd184a0dbd10cbf66fef190f1d897026863449afa56fa0de4d34cb594659d00a93e5eab2ae6ec548050dbcb3ce20321d2
data/.rubocop.yml CHANGED
@@ -9,6 +9,9 @@ Layout/FirstArrayElementIndentation:
9
9
  Layout/LineLength:
10
10
  Enabled: false
11
11
 
12
+ Layout/MultilineMethodCallIndentation:
13
+ EnforcedStyle: indented
14
+
12
15
  Metrics/AbcSize:
13
16
  Enabled: false
14
17
 
@@ -27,6 +30,13 @@ Metrics/ModuleLength:
27
30
  Naming/VariableNumber:
28
31
  EnforcedStyle: snake_case
29
32
 
33
+ Style/BlockDelimiters:
34
+ EnforcedStyle: always_braces
35
+ AllowedMethods:
36
+ - describe
37
+ - context
38
+ - it
39
+
30
40
  Style/Documentation:
31
41
  Enabled: false
32
42
 
data/README.md CHANGED
@@ -1,5 +1,35 @@
1
1
  # Mangrove
2
- Mangrove provides utility types to use with Sorbet
2
+ Mangrove provides type utility to use with Sorbet.
3
+
4
+ You can do something like this with the gem.
5
+
6
+ ```ruby
7
+ class MyClass
8
+ extend T::Sig
9
+
10
+ include Mangrove::ControlFlow::Handler
11
+
12
+ sig { params(numbers: T::Enumerable[Integer]).returns(Mangrove::Result[T::Array[Integer], String]) }
13
+ def divide_arguments_by_3(numbers)
14
+ divided_nubmers = numbers
15
+ .map { |number|
16
+ if number % 3 == 0
17
+ Mangrove::Result::Ok.new(number / 3)
18
+ else
19
+ Mangrove::Result::Err.new("number #{number} is not divisible by 3")
20
+ end
21
+ }
22
+ .map(&:unwrap!)
23
+
24
+ Mangrove::Result::Ok.new(divided_nubmers)
25
+ end
26
+ end
27
+
28
+ expect(MyClass.new.divide_arguments_by_3([3, 4, 6])).to eq Mangrove::Result::Err.new("number 4 is not divisible by 3")
29
+ expect(MyClass.new.divide_arguments_by_3([3, 6, 9])).to eq Mangrove::Result::Ok.new([1, 2, 3])
30
+ ```
31
+
32
+ Other examples are available at `spec/**/**_spec.rb`.
3
33
 
4
34
  ## Features
5
35
  Most features are not implemented.
@@ -10,24 +40,34 @@ Most features are not implemented.
10
40
  - [x] Auto propagation like Rust's `?` (formerly `try!`)
11
41
  - [ ] Builder type (Not implemented)
12
42
  - [ ] Auto Implementation
13
-
14
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mangrove`. To experiment with that code, run `bin/console` for an interactive prompt.
43
+ - [ ] TODO
15
44
 
16
45
  ## Installation
17
46
 
18
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
19
-
20
- Install the gem and add to the application's Gemfile by executing:
47
+ ```
48
+ bundle add mangrove
49
+ ```
21
50
 
22
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
51
+ ## Usage
23
52
 
24
- If bundler is not being used to manage dependencies, install the gem by executing:
53
+ see `spec/**/**_spec.rb`.
25
54
 
26
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
55
+ ```ruby
56
+ Mangrove::Result[OkType, ErrType]
57
+ Mangrove::Result::Ok[OkType, ErrType]
58
+ Mangrove::Result::Err[OkType, ErrType]
59
+ Mangrove::Option[InnerType]
60
+ Mangrove::Option::Some[InnerType]
61
+ Mangrove::Option::None[InnerType]
27
62
 
28
- ## Usage
63
+ my_ok = Result::Ok.new("my value")
64
+ my_err = Result::Err.new("my err")
65
+ my_some = Option::Some.new(1234)
66
+ my_none = Option::None.new
29
67
 
30
- TODO: Write usage instructions here
68
+ # Including this Module into your class appends rescue clause into its methods. Results to `Option#unwrap!` and `Result#unwrap!` propagates to calling method like Ruet's `?` operator.
69
+ include Mangrove::ControlFlow::Handler
70
+ ```
31
71
 
32
72
  ## Commands
33
73
  ```
@@ -53,4 +93,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
53
93
 
54
94
  ## Contributing
55
95
 
56
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mangrove.
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kazzix14/mangrove.
data/Rakefile CHANGED
@@ -3,12 +3,12 @@
3
3
  require "bundler/gem_tasks"
4
4
  require "rake/testtask"
5
5
 
6
- task :check do
6
+ task(:check) {
7
7
  system("bundle exec ordinare --check") &&
8
8
  system("bundle exec rubocop -DESP") &&
9
9
  system("bundle exec tapioca check-shims") &&
10
10
  system("bundle exec srb typecheck") &&
11
11
  system("bundle exec rspec -f d")
12
- end
12
+ }
13
13
 
14
14
  task default: :check
@@ -127,7 +127,7 @@ module Mangrove
127
127
  end
128
128
 
129
129
  def rescue_body_node
130
- control_flow = StandardError
130
+ control_flow = Mangrove::ControlFlow::ControlSignal
131
131
 
132
132
  ::Parser::AST::Node.new(:resbody, [
133
133
  ::Parser::AST::Node.new(:array, [
@@ -143,7 +143,10 @@ module Mangrove
143
143
  :Err
144
144
  ]),
145
145
  :new,
146
- ::Parser::AST::Node.new(:lvar, [:exception])
146
+ ::Parser::AST::Node.new(:send, [
147
+ ::Parser::AST::Node.new(:lvar, [:exception]),
148
+ :inner_value
149
+ ])
147
150
  ])
148
151
  ])
149
152
  end
@@ -113,12 +113,12 @@ module Mangrove
113
113
 
114
114
  sig { override.returns(OkType) }
115
115
  def unwrap!
116
- raise Result::ControlSignal, Result::Err.new("called `Result#unwrap!` on an `Err` value: #{self}")
116
+ raise Result::ControlSignal, @inner
117
117
  end
118
118
 
119
119
  sig { override.params(message: String).returns(OkType) }
120
120
  def expect!(message)
121
- raise Result::ControlSignal, Result::Err.new(message)
121
+ raise Result::ControlSignal, message
122
122
  end
123
123
 
124
124
  sig { override.returns(T::Boolean) }
@@ -127,16 +127,12 @@ module Mangrove
127
127
  sig { override.returns(T::Boolean) }
128
128
  def err? = true
129
129
 
130
- sig do
131
- override.params(_block: T.proc.params(this: OkType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType])
132
- end
130
+ sig { override.params(_block: T.proc.params(this: OkType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType]) }
133
131
  def map_ok(&_block)
134
132
  self
135
133
  end
136
134
 
137
- sig do
138
- override.params(block: T.proc.params(this: ErrType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType])
139
- end
135
+ sig { override.params(block: T.proc.params(this: ErrType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType]) }
140
136
  def map_err(&block)
141
137
  block.call(@inner)
142
138
  end
@@ -157,16 +153,10 @@ module Mangrove
157
153
  sig { abstract.params(message: String).returns(OkType) }
158
154
  def expect!(message); end
159
155
 
160
- sig do
161
- abstract.params(block: T.proc.params(this: OkType).returns(Result[OkType,
162
- ErrType])).returns(Result[OkType, ErrType])
163
- end
156
+ sig { abstract.params(block: T.proc.params(this: OkType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType]) }
164
157
  def map_ok(&block); end
165
158
 
166
- sig do
167
- abstract.params(block: T.proc.params(this: ErrType).returns(Result[OkType,
168
- ErrType])).returns(Result[OkType, ErrType])
169
- end
159
+ sig { abstract.params(block: T.proc.params(this: ErrType).returns(Result[OkType, ErrType])).returns(Result[OkType, ErrType]) }
170
160
  def map_err(&block); end
171
161
  end
172
162
  end
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Mangrove
5
- VERSION = "0.3.0"
5
+ VERSION = "0.4.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mangrove
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kazuma Murata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-25 00:00:00.000000000 Z
11
+ date: 2023-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sorbet-runtime