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/.gitignore +9 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +30 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +74 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/nilable.rb +25 -0
- data/lib/nilable/kernel.rb +5 -0
- data/lib/nilable/version.rb +3 -0
- data/nilable.gemspec +25 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f34025249959bb94ff93e7bbc004ae32d8359975
|
4
|
+
data.tar.gz: 0849a912bb235455fd8084cec523daa389758f58
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3ee7dfb2e14b1706e7931bc2e2d6ee719fb51e6436e257e3a2c30c647163a446f751c78c2fb4c8f77e822442889c9fca26d1b62c8e3e3d31c661b823151cd5ab
|
7
|
+
data.tar.gz: 1a0ad64a5936843b1947704495ed4181acc0fa6d3159db80ab1c7d238fd0cd8f7f997da71cef74951cbfebd50066db741fdd111e60d5ea2d69d2d860ab0fec7f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all
|
4
|
+
people who contribute through reporting issues, posting feature requests,
|
5
|
+
updating documentation, submitting pull requests or patches, and other
|
6
|
+
activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, age, or religion.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include the use of sexual
|
14
|
+
language or imagery, derogatory comments or personal attacks, trolling, public
|
15
|
+
or private harassment, insults, or other unprofessional conduct.
|
16
|
+
|
17
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
18
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
19
|
+
that are not aligned to this Code of Conduct. Project maintainers who do not
|
20
|
+
follow the Code of Conduct may be removed from the project team.
|
21
|
+
|
22
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
23
|
+
reported by opening an issue or contacting one or more of the project
|
24
|
+
maintainers.
|
25
|
+
|
26
|
+
This Code of Conduct is adapted from the [Contributor Covenant] version
|
27
|
+
1.0.0, available at [http://contributor-covenant.org/version/1/0/0/].
|
28
|
+
|
29
|
+
[Contributor Covenant]: http:contributor-covenant.org
|
30
|
+
[http://contributor-covenant.org/version/1/0/0/]: http://contributor-covenant.org/version/1/0/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Genadi Samokovarov
|
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,74 @@
|
|
1
|
+
# Nilable
|
2
|
+
|
3
|
+
Nilable object is a tool to handle nil invocations.
|
4
|
+
|
5
|
+
Any nilable object wraps a single value object and proxy method invocations to
|
6
|
+
it. In turn, every method result is wrapped in an nilable object.
|
7
|
+
|
8
|
+
If somewhere along the call chain, a method result is `nil`, no `NoMethodError`
|
9
|
+
will be raised and you can keep on chaining method calls. It acts as a black
|
10
|
+
hole object.
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'nilable'
|
18
|
+
```
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Nilable objects come to the rescue, when you're working on legacy code bases,
|
23
|
+
where you can't avoid the nils and you have to deal with them. In fresh
|
24
|
+
projects, you don't wanna use nilable objects, but avoid leaking the nils in
|
25
|
+
the first place. With that out of the way, here is how you can use the nilable
|
26
|
+
objects.
|
27
|
+
|
28
|
+
Imagine a legacy system where an user has an account. There are no database
|
29
|
+
constraints and `User#account` can always be nil. In fact, it already is in old
|
30
|
+
production users.
|
31
|
+
|
32
|
+
Every time you get to work with an user object and have to get its account, you
|
33
|
+
have to check whether its nil. Even worse, if the account has nilable fields as
|
34
|
+
well, you have to check them too:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
def format_currency(user)
|
38
|
+
if account = user.account
|
39
|
+
if currency = account.currency
|
40
|
+
currency.format
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Forget one check and you break. Forget a test and you break in production.
|
47
|
+
|
48
|
+
In such hostile systems, you can use the nilable objects to save yourself all
|
49
|
+
those checks. Wrap your hostile objects and call your methods away. If a `nil`
|
50
|
+
happens anywhere in the call chain, another nilable object will be returned.
|
51
|
+
When done, call `Nilable#value` to extract the value out of the nilable object.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
def format_currency(user)
|
55
|
+
Nilable(user.account).currency.format.value
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
That's it. Wrap your hostile objects in nilable and have your newer code free
|
60
|
+
of defensive nil checks.
|
61
|
+
|
62
|
+
## Credits
|
63
|
+
|
64
|
+
What I call a nilable object, is well documented in the wild as the [Option
|
65
|
+
type]. There are many implementations of it in Ruby land, with the most popular
|
66
|
+
of them being [Tom Stuart]'s [monads]. If you need more utils to deal with your
|
67
|
+
nils, check it out.
|
68
|
+
|
69
|
+
Where nilable shines for me, is the simple implementation. That's all I need
|
70
|
+
for my legacy projects.
|
71
|
+
|
72
|
+
[Option type]: https://en.wikipedia.org/wiki/Option_type
|
73
|
+
[monads]: https://github.com/tomstuart/monads
|
74
|
+
[Tom Stuart]: https://github.com/tomstuart
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "nilable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/nilable.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'nilable/kernel'
|
2
|
+
|
3
|
+
# Nilable object is a tool to handle nil invocations.
|
4
|
+
#
|
5
|
+
# Any Nilable object wraps a single value object and proxy method
|
6
|
+
# invocations to it. In turn, every method result is wrapped in an Nilable
|
7
|
+
# object.
|
8
|
+
#
|
9
|
+
# That way, if somewhere along the call chain, method result is `nil`, no
|
10
|
+
# `NoMethodError` will be raised. It acts as a black hole object.
|
11
|
+
class Nilable < BasicObject
|
12
|
+
attr_reader :value
|
13
|
+
|
14
|
+
def initialize(object)
|
15
|
+
@value = object.is_a?(::Nilable) ? object.value : object
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(name, *args, &block)
|
19
|
+
if value
|
20
|
+
::Nilable.new(value.public_send(name, *args, &block))
|
21
|
+
else
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/nilable.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nilable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nilable"
|
8
|
+
spec.version = Nilable::VERSION
|
9
|
+
spec.authors = ["Genadi Samokovarov"]
|
10
|
+
spec.email = ["gsamokovarov@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Nilable object is a tool to handle nil invocations."
|
13
|
+
spec.description = "Nilable object is a tool to handle nil invocations."
|
14
|
+
spec.homepage = "https://github.com/gsamokovarov/nilable"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.4"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nilable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genadi Samokovarov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.4'
|
55
|
+
description: Nilable object is a tool to handle nil invocations.
|
56
|
+
email:
|
57
|
+
- gsamokovarov@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/nilable.rb
|
72
|
+
- lib/nilable/kernel.rb
|
73
|
+
- lib/nilable/version.rb
|
74
|
+
- nilable.gemspec
|
75
|
+
homepage: https://github.com/gsamokovarov/nilable
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.4.5
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Nilable object is a tool to handle nil invocations.
|
99
|
+
test_files: []
|