forgiving_nil 0.0.1
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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +10 -0
- data/Rakefile +1 -0
- data/forgiving_nil.gemspec +22 -0
- data/lib/forgiving_nil/nil_class.rb +14 -0
- data/lib/forgiving_nil/version.rb +3 -0
- data/lib/forgiving_nil.rb +5 -0
- data/spec/forgiving_nil_spec.rb +13 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
```ruby
|
|
2
|
+
some_obj.big.long.chain.of.methods.that.breaks.because.of.a.nil
|
|
3
|
+
# => Exception, nil or the result of the last method call
|
|
4
|
+
|
|
5
|
+
!some_obj.big.nil? && !some_obj.big.long.nil? && !some_obj.big.long.chain.nil? && !some_obj.big.long.chain.of.nil? #.....................
|
|
6
|
+
# => lame
|
|
7
|
+
|
|
8
|
+
nil.forgive { some_obj.big.long.chain.of.methods.that.breaks.because.of.a.nil }
|
|
9
|
+
# => nil, or the result of the last method call
|
|
10
|
+
```
|
data/Rakefile
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
3
|
+
require "forgiving_nil/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = "forgiving_nil"
|
|
7
|
+
s.version = ForgivingNil::VERSION
|
|
8
|
+
s.authors = ["Finbarr Taylor"]
|
|
9
|
+
s.email = ["finbarrtaylor@gmail.com"]
|
|
10
|
+
s.homepage = ""
|
|
11
|
+
s.summary = %q{Forgiving Nil lets you forget about nil exceptions conveniently and temporarily when required}
|
|
12
|
+
s.description = %q{You can simply ask nil to forgive you: nil.forgive{real_object.might_be_nil.dont.care} #=> nil or the value of care}
|
|
13
|
+
|
|
14
|
+
s.rubyforge_project = "forgiving_nil"
|
|
15
|
+
|
|
16
|
+
s.files = `git ls-files`.split("\n")
|
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
19
|
+
s.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
|
22
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class NilClass
|
|
2
|
+
attr_accessor :forgiving
|
|
3
|
+
def method_missing(name, *args, &block)
|
|
4
|
+
return self if @forgiving
|
|
5
|
+
super
|
|
6
|
+
end
|
|
7
|
+
def forgive
|
|
8
|
+
return super unless block_given?
|
|
9
|
+
@forgiving = true
|
|
10
|
+
result = yield
|
|
11
|
+
@forgiving = false
|
|
12
|
+
result
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require "forgiving_nil/nil_class"
|
|
2
|
+
describe ForgivingNil do
|
|
3
|
+
it "has no effect by default" do
|
|
4
|
+
lambda { nil.foo }.should raise_error
|
|
5
|
+
end
|
|
6
|
+
it "forgives nil" do
|
|
7
|
+
nil.forgive { nil.foo.bar }.should be_nil
|
|
8
|
+
end
|
|
9
|
+
it "only forgives temporarily" do
|
|
10
|
+
nil.forgive { nil.foo.bar }
|
|
11
|
+
lambda { nil.foo }.should raise_error
|
|
12
|
+
end
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: forgiving_nil
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Finbarr Taylor
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2012-01-24 00:00:00.000000000Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: rspec
|
|
16
|
+
requirement: &2163036200 !ruby/object:Gem::Requirement
|
|
17
|
+
none: false
|
|
18
|
+
requirements:
|
|
19
|
+
- - ~>
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '2.6'
|
|
22
|
+
type: :development
|
|
23
|
+
prerelease: false
|
|
24
|
+
version_requirements: *2163036200
|
|
25
|
+
description: ! 'You can simply ask nil to forgive you: nil.forgive{real_object.might_be_nil.dont.care}
|
|
26
|
+
#=> nil or the value of care'
|
|
27
|
+
email:
|
|
28
|
+
- finbarrtaylor@gmail.com
|
|
29
|
+
executables: []
|
|
30
|
+
extensions: []
|
|
31
|
+
extra_rdoc_files: []
|
|
32
|
+
files:
|
|
33
|
+
- .gitignore
|
|
34
|
+
- Gemfile
|
|
35
|
+
- README.md
|
|
36
|
+
- Rakefile
|
|
37
|
+
- forgiving_nil.gemspec
|
|
38
|
+
- lib/forgiving_nil.rb
|
|
39
|
+
- lib/forgiving_nil/nil_class.rb
|
|
40
|
+
- lib/forgiving_nil/version.rb
|
|
41
|
+
- spec/forgiving_nil_spec.rb
|
|
42
|
+
homepage: ''
|
|
43
|
+
licenses: []
|
|
44
|
+
post_install_message:
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
none: false
|
|
50
|
+
requirements:
|
|
51
|
+
- - ! '>='
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
none: false
|
|
56
|
+
requirements:
|
|
57
|
+
- - ! '>='
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: '0'
|
|
60
|
+
requirements: []
|
|
61
|
+
rubyforge_project: forgiving_nil
|
|
62
|
+
rubygems_version: 1.8.10
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 3
|
|
65
|
+
summary: Forgiving Nil lets you forget about nil exceptions conveniently and temporarily
|
|
66
|
+
when required
|
|
67
|
+
test_files:
|
|
68
|
+
- spec/forgiving_nil_spec.rb
|