ssoroka-ignore_nil 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,28 +2,64 @@
2
2
 
3
3
  == DESCRIPTION
4
4
 
5
- ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects.
5
+ ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much
6
+ safer than "rescue nil", since it only catches NoMethodError on nil objects.
6
7
 
7
- ignore_nil {} will either return the last thing evaluated in the block, or nil if a NoMethodError is raised calling a method on a nil object. Any other exceptions raised in the block are not handled, and left for the application to resolve.
8
+ ignore_nil {} will either return the last thing evaluated in the block, or nil if a NoMethodError
9
+ is raised calling a method on a nil object. Any other exceptions raised in the block are not handled,
10
+ and left for the application to resolve. More details below.
8
11
 
9
12
  == INSTALLATION
10
13
 
11
14
  as a gem:
12
- sudo gem install ssoroka-ignore_nil
15
+
16
+ sudo gem install ssoroka-ignore_nil
13
17
 
14
18
  as a plugin:
15
- script/plugin install git://github.com/ssoroka/ignore_nil.git
19
+
20
+ script/plugin install git://github.com/ssoroka/ignore_nil.git
16
21
 
17
22
  == USAGE
18
23
 
19
- ignore_nil { user.profile.photo }
24
+ ignore_nil { user.profile.photo }
20
25
 
21
26
  which is much cleaner than, say,
22
27
 
23
- user && user.profile && user.profile.photo
28
+ user && user.profile && user.profile.photo
24
29
 
25
30
  and much much safer than
26
31
 
27
- user.profile.photo rescue nil
32
+ user.profile.photo rescue nil
28
33
 
29
- which will eat any error, even if it's one you really want to see.
34
+ which will eat any error, even if it's one you really want to see.
35
+
36
+ == TELL ME MORE!
37
+
38
+ The plugin is really rather simple; here's the ignore_nil method:
39
+
40
+ def ignore_nil(&block)
41
+ begin
42
+ yield
43
+ rescue NoMethodError, RuntimeError => e
44
+ if (e.message =~ /You have a nil object when you didn't expect it/) ||
45
+ (e.message =~ /undefined method `.*?' for nil:NilClass/) || (e.message =~ /^Called id for nil/)
46
+ return nil
47
+ else
48
+ raise e
49
+ end
50
+ end
51
+ end
52
+
53
+ What's interesting about this is it catches both NoMethodError and RuntimeError, both of which
54
+ can occur if a method unexpectedly returned nil and you called a method on it, but *ONLY* if
55
+ the error message matches! This means legitimate NoMethodError and RuntimeError messages will
56
+ not be bothered by ignore_nil, and will still raise in your application as you expect.
57
+
58
+ I've used this in a production application since about mid/late 2008, I'd consider it very stable.
59
+ Feedback welcome!
60
+
61
+ == AUTHOR
62
+
63
+ Steven Soroka
64
+ [@ssoroka](http://www.twitter.com/ssoroka)
65
+ [http://blog.stevensoroka.ca](http://blog.stevensoroka.ca)
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('ignore_nil', '1.0.1') do |p|
5
+ Echoe.new('ignore_nil', '1.0.2') do |p|
6
6
  p.description = 'ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects'
7
7
  p.url = 'http://github.com/ssoroka/ignore_nil'
8
8
  p.author = 'Steven Soroka'
@@ -1,28 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = %q{ignore_nil}
3
- s.version = "1.0.1"
5
+ s.version = "1.0.2"
4
6
 
5
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
6
8
  s.authors = ["Steven Soroka"]
7
- s.date = %q{2008-12-11}
9
+ s.date = %q{2009-09-23}
8
10
  s.description = %q{ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects}
9
11
  s.email = %q{ssoroka78@gmail.com}
10
12
  s.extra_rdoc_files = ["lib/ignore_nil.rb", "README.rdoc"]
11
- s.files = ["ignore_nil.gemspec", "init.rb", "lib/ignore_nil.rb", "Manifest", "Rakefile", "README.rdoc", "test/ignore_nil_test.rb"]
12
- s.has_rdoc = true
13
+ s.files = ["ignore_nil.gemspec", "init.rb", "lib/ignore_nil.rb", "Rakefile", "README.rdoc", "test/ignore_nil_test.rb"]
13
14
  s.homepage = %q{http://github.com/ssoroka/ignore_nil}
14
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Ignore_nil", "--main", "README.rdoc"]
15
16
  s.require_paths = ["lib"]
16
17
  s.rubyforge_project = %q{ignore_nil}
17
- s.rubygems_version = %q{1.2.0}
18
+ s.rubygems_version = %q{1.3.3}
18
19
  s.summary = %q{ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects}
19
20
  s.test_files = ["test/ignore_nil_test.rb"]
20
21
 
21
22
  if s.respond_to? :specification_version then
22
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
- s.specification_version = 2
24
+ s.specification_version = 3
24
25
 
25
- if current_version >= 3 then
26
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
27
  else
27
28
  end
28
29
  else
data/init.rb CHANGED
@@ -1 +1 @@
1
- require File.join(File.dirname(__FILE__),'lib','ignore_nil.rb')
1
+ require File.join(File.dirname(__FILE__), 'lib', 'ignore_nil')
@@ -1,4 +1,5 @@
1
1
  module IgnoreNil
2
+ # ignore NoMethodError on nil
2
3
  def ignore_nil(&block)
3
4
  begin
4
5
  yield
@@ -11,9 +12,6 @@ module IgnoreNil
11
12
  end
12
13
  end
13
14
  end
14
- alias :ignore_nils :ignore_nil
15
- alias :quiet_nil :ignore_nil
16
- alias :ignore_no_method_on_nil :ignore_nil
17
15
  end
18
16
 
19
17
  Object.send(:include, IgnoreNil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ssoroka-ignore_nil
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Soroka
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-12-11 00:00:00 -08:00
12
+ date: 2009-09-23 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,12 +26,12 @@ files:
26
26
  - ignore_nil.gemspec
27
27
  - init.rb
28
28
  - lib/ignore_nil.rb
29
- - Manifest
30
29
  - Rakefile
31
30
  - README.rdoc
32
31
  - test/ignore_nil_test.rb
33
- has_rdoc: true
32
+ has_rdoc: false
34
33
  homepage: http://github.com/ssoroka/ignore_nil
34
+ licenses:
35
35
  post_install_message:
36
36
  rdoc_options:
37
37
  - --line-numbers
@@ -57,9 +57,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
57
  requirements: []
58
58
 
59
59
  rubyforge_project: ignore_nil
60
- rubygems_version: 1.2.0
60
+ rubygems_version: 1.3.5
61
61
  signing_key:
62
- specification_version: 2
62
+ specification_version: 3
63
63
  summary: ignore_nil lets you happily ignore nil methods on long method chains. Keeps code pretty and much safer than "rescue nil", since it only catches NoMethodError on nil objects
64
64
  test_files:
65
65
  - test/ignore_nil_test.rb