not_nilable 1.0.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/lib/not_nilable/core_ext/nil_class.rb +14 -0
- data/lib/not_nilable/core_ext/object.rb +15 -0
- data/lib/not_nilable/version.rb +5 -0
- data/lib/not_nilable.rb +11 -0
- data/sig/not_nilable/core_ext/nil_class.rbs +12 -0
- data/sig/not_nilable/core_ext/object.rbs +13 -0
- data/sig/not_nilable/version.rbs +5 -0
- data/sig/not_nilable.rbs +6 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 13334576efbfe3d36a848151d4b41fde9a624b83748cbaad0e19f14c558eeb3e
|
|
4
|
+
data.tar.gz: 10af223a1f5f660b98e90d0a67aee35b3f6bc45e795a2f2a2bcd5642af24eb01
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 225f3ca2c4a5d438466dc7359555c2f153582d59259168b111ccdfbebdaff285d9f713e2be867edf05eab1eb38b3c6bb35f56a3b53f52b5e1a4a91ddc30bfee2
|
|
7
|
+
data.tar.gz: 047ec5e9324c0c19bf062f6efdffc5e417bd7e9e474a114ff899f62eb81f4d1ac2dbaa31efc02bc5b9cc973c116a6a9721c4710b8327340388b2377b24864f19
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"not_nilable" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["i.tkomiya@gmail.com"](mailto:"i.tkomiya@gmail.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Takeshi KOMIYA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# NotNilable
|
|
2
|
+
|
|
3
|
+
A non-null assertion operator for Ruby. Adds `Object#not_nil!`, ported from TypeScript's `!` operator and inspired by [Crystal's `not_nil!`](https://crystal-lang.org/api/Object.html#not_nil!).
|
|
4
|
+
|
|
5
|
+
When the receiver is anything but `nil`, `.not_nil!` returns the receiver unchanged. When it is `nil`, the method raises `NotNilable::NilAssertionError`. The bundled RBS signatures let [Steep](https://github.com/soutaro/steep) narrow `T?` to `T` at the type level.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bundle add not_nilable
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require "not_nilable"
|
|
17
|
+
|
|
18
|
+
"hello".not_nil! # => "hello"
|
|
19
|
+
42.not_nil! # => 42
|
|
20
|
+
|
|
21
|
+
nil.not_nil!
|
|
22
|
+
# raises NotNilable::NilAssertionError: Nil assertion failed
|
|
23
|
+
|
|
24
|
+
nil.not_nil!("user must be present")
|
|
25
|
+
# raises NotNilable::NilAssertionError: user must be present
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
With RBS + Steep, the type is narrowed:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
val = ENV.fetch("HOME", nil) # type: String?
|
|
32
|
+
val.not_nil!.length # narrowed to String — .length is OK
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Development
|
|
36
|
+
|
|
37
|
+
After cloning the repo, run `bundle install`, then `bundle exec rake` to lint, run specs, and type-check.
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
[MIT](LICENSE.txt).
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class NilClass
|
|
4
|
+
# Raises +NotNilable::NilAssertionError+.
|
|
5
|
+
#
|
|
6
|
+
# This method asserts that the receiver is not +nil+. Since the receiver
|
|
7
|
+
# _is_ +nil+, the assertion fails and an exception is raised. An optional
|
|
8
|
+
# +message+ may be supplied to customize the exception message.
|
|
9
|
+
#
|
|
10
|
+
# @rbs message: String?
|
|
11
|
+
def not_nil!(message = nil) #: bot
|
|
12
|
+
raise NotNilable::NilAssertionError, message || "Nil assertion failed"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Object
|
|
4
|
+
# Returns +self+.
|
|
5
|
+
#
|
|
6
|
+
# This method asserts that the receiver is not +nil+. Since +Object+ is not
|
|
7
|
+
# +NilClass+, the assertion succeeds and +self+ is returned. The +message+
|
|
8
|
+
# parameter is accepted for API parity with +NilClass#not_nil!+ but is
|
|
9
|
+
# ignored.
|
|
10
|
+
#
|
|
11
|
+
# @rbs message: String?
|
|
12
|
+
def not_nil!(message = nil) #: self # rubocop:disable Lint/UnusedMethodArgument
|
|
13
|
+
self
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/not_nilable.rb
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Generated from lib/not_nilable/core_ext/nil_class.rb with RBS::Inline
|
|
2
|
+
|
|
3
|
+
class NilClass
|
|
4
|
+
# Raises +NotNilable::NilAssertionError+.
|
|
5
|
+
#
|
|
6
|
+
# This method asserts that the receiver is not +nil+. Since the receiver
|
|
7
|
+
# _is_ +nil+, the assertion fails and an exception is raised. An optional
|
|
8
|
+
# +message+ may be supplied to customize the exception message.
|
|
9
|
+
#
|
|
10
|
+
# @rbs message: String?
|
|
11
|
+
def not_nil!: (?String? message) -> bot
|
|
12
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Generated from lib/not_nilable/core_ext/object.rb with RBS::Inline
|
|
2
|
+
|
|
3
|
+
class Object
|
|
4
|
+
# Returns +self+.
|
|
5
|
+
#
|
|
6
|
+
# This method asserts that the receiver is not +nil+. Since +Object+ is not
|
|
7
|
+
# +NilClass+, the assertion succeeds and +self+ is returned. The +message+
|
|
8
|
+
# parameter is accepted for API parity with +NilClass#not_nil!+ but is
|
|
9
|
+
# ignored.
|
|
10
|
+
#
|
|
11
|
+
# @rbs message: String?
|
|
12
|
+
def not_nil!: (?String? message) -> self
|
|
13
|
+
end
|
data/sig/not_nilable.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: not_nilable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Takeshi KOMIYA
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Ports TypeScript's non-null assertion `!` to Ruby as a method, inspired
|
|
13
|
+
by Crystal's `not_nil!`. The bundled RBS signatures let Steep narrow `T?` to `T`
|
|
14
|
+
at the type level.
|
|
15
|
+
email:
|
|
16
|
+
- i.tkomiya@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- CODE_OF_CONDUCT.md
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/not_nilable.rb
|
|
26
|
+
- lib/not_nilable/core_ext/nil_class.rb
|
|
27
|
+
- lib/not_nilable/core_ext/object.rb
|
|
28
|
+
- lib/not_nilable/version.rb
|
|
29
|
+
- sig/not_nilable.rbs
|
|
30
|
+
- sig/not_nilable/core_ext/nil_class.rbs
|
|
31
|
+
- sig/not_nilable/core_ext/object.rbs
|
|
32
|
+
- sig/not_nilable/version.rbs
|
|
33
|
+
homepage: https://github.com/tk0miya/not_nilable
|
|
34
|
+
licenses:
|
|
35
|
+
- MIT
|
|
36
|
+
metadata:
|
|
37
|
+
allowed_push_host: https://rubygems.org
|
|
38
|
+
source_code_uri: https://github.com/tk0miya/not_nilable
|
|
39
|
+
changelog_uri: https://github.com/tk0miya/not_nilable/blob/main/CHANGELOG.md
|
|
40
|
+
rubygems_mfa_required: 'true'
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '3.3'
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 4.0.10
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: 'A non-null assertion operator for Ruby: Object#not_nil!.'
|
|
58
|
+
test_files: []
|