easy_nils 0.0.2
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 +6 -0
- data/LICENSE +20 -0
- data/README.rdoc +42 -0
- data/Rakefile +72 -0
- data/init.rb +1 -0
- data/lib/easy_nils.rb +36 -0
- data/spec/easy_nils_spec.rb +39 -0
- data/spec/enable_disable_spec.rb +22 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +3 -0
- metadata +87 -0
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006-2009 Jeff Patmon
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
= EasyNils
|
2
|
+
|
3
|
+
Why write this
|
4
|
+
|
5
|
+
user && user.account && user.account.authorized?
|
6
|
+
|
7
|
+
when what you want is this
|
8
|
+
|
9
|
+
user.account.authorized?
|
10
|
+
|
11
|
+
EasyNils lets you abridge compound conditional expressions returning nil if any of the method calls return nil.
|
12
|
+
|
13
|
+
== Resources
|
14
|
+
|
15
|
+
Install
|
16
|
+
|
17
|
+
* sudo gem install easy_nils
|
18
|
+
|
19
|
+
Use
|
20
|
+
|
21
|
+
* require 'easy_nils'
|
22
|
+
|
23
|
+
Rails
|
24
|
+
|
25
|
+
* gem.config 'easy_nils'
|
26
|
+
|
27
|
+
== Usage
|
28
|
+
|
29
|
+
If you need to explicitly test for a nil value, then do so
|
30
|
+
|
31
|
+
if user.account.authorized?
|
32
|
+
... do something for an authorized account
|
33
|
+
elsif user.nil? || user.account.nil?
|
34
|
+
... do something here for the nil case
|
35
|
+
else
|
36
|
+
... do someting for an unauthorized account
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
== Dependencies
|
41
|
+
|
42
|
+
* meta_programming
|
data/Rakefile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
#require 'rake/testtask'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
gem 'gem_version', '>= 0.0.1'
|
5
|
+
require 'gem_version'
|
6
|
+
#require 'rake/contrib/sshpublisher'
|
7
|
+
|
8
|
+
spec = Gem::Specification.new do |s|
|
9
|
+
s.name = 'easy_nils'
|
10
|
+
s.version = GemVersion.next_version
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.required_ruby_version = '>= 1.8.7'
|
13
|
+
s.description = 'Simpler nil handling.'
|
14
|
+
s.summary = 'Abridge compound conditional statements to a single expression.'
|
15
|
+
|
16
|
+
s.add_dependency('meta_programming', '>= 0.0.6')
|
17
|
+
|
18
|
+
exclude_folders = '' # 'spec/rails/{doc,lib,log,nbproject,tmp,vendor,test}'
|
19
|
+
exclude_files = [] # FileList['**/*.log'] + FileList[exclude_folders+'/**/*'] + FileList[exclude_folders]
|
20
|
+
s.files = FileList['{lib,spec}/**/*'] + %w(init.rb LICENSE Rakefile README.rdoc .gitignore) - exclude_files
|
21
|
+
s.require_path = 'lib'
|
22
|
+
s.has_rdoc = true
|
23
|
+
s.test_files = Dir['spec/*_spec.rb']
|
24
|
+
|
25
|
+
s.author = 'Jeff Patmon'
|
26
|
+
s.email = 'jpatmon@gmail.com'
|
27
|
+
s.homepage = 'http://github.com/jeffp/easy_nils/tree/master'
|
28
|
+
end
|
29
|
+
|
30
|
+
require 'spec/version'
|
31
|
+
require 'spec/rake/spectask'
|
32
|
+
|
33
|
+
desc "Run specs"
|
34
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
35
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
36
|
+
t.libs << 'lib' << 'spec'
|
37
|
+
t.rcov = false
|
38
|
+
t.spec_opts = ['--options', 'spec/spec.opts']
|
39
|
+
#t.rcov_dir = 'coverage'
|
40
|
+
#t.rcov_opts = ['--exclude', "kernel,load-diff-lcs\.rb,instance_exec\.rb,lib/spec.rb,lib/spec/runner.rb,^spec/*,bin/spec,examples,/gems,/Library/Ruby,\.autotest,#{ENV['GEM_HOME']}"]
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Clean Gem build'
|
44
|
+
task :clean do
|
45
|
+
FileUtils.rm_f Dir.glob('*.gem')
|
46
|
+
end
|
47
|
+
|
48
|
+
desc "Generate documentation for the #{spec.name} gem."
|
49
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
50
|
+
rdoc.rdoc_dir = 'rdoc'
|
51
|
+
rdoc.title = spec.name
|
52
|
+
#rdoc.template = '../rdoc_template.rb'
|
53
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
54
|
+
rdoc.rdoc_files.include('README.rdoc', 'LICENSE', 'lib/**/*.rb')
|
55
|
+
end
|
56
|
+
|
57
|
+
desc 'Generate a gemspec file.'
|
58
|
+
task :gemspec do
|
59
|
+
File.open("#{spec.name}.gemspec", 'w') do |f|
|
60
|
+
f.write spec.to_ruby
|
61
|
+
end
|
62
|
+
GemVersion.increment_version
|
63
|
+
GemVersion.commit_and_push
|
64
|
+
end
|
65
|
+
|
66
|
+
Rake::GemPackageTask.new(spec) do |p|
|
67
|
+
p.gem_spec = spec
|
68
|
+
p.need_tar = RUBY_PLATFORM =~ /mswin/ ? false : true
|
69
|
+
p.need_zip = true
|
70
|
+
end
|
71
|
+
|
72
|
+
Dir['tasks/**/*.rake'].each {|rake| load rake}
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'arspy'
|
data/lib/easy_nils.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'meta_programming', '>= 0.0.6'
|
3
|
+
require 'meta_programming'
|
4
|
+
|
5
|
+
class NilClass
|
6
|
+
def not_nil?; false; end
|
7
|
+
@@warnings = false
|
8
|
+
@@disable_easy_nils = false
|
9
|
+
define_method_missing_chain(:easy_nils) do |sym, *args|
|
10
|
+
unless @@disable_easy_nils
|
11
|
+
nil
|
12
|
+
else
|
13
|
+
method_missing_without_easy_nils(sym, *args)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.warnings(flag)
|
18
|
+
@@warnings = flag
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.disable_easy_nils(flag)
|
22
|
+
@@disable_easy_nils = flag
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Kernel
|
27
|
+
def enable_easy_nils_warnings(flag)
|
28
|
+
NilClass.warnings(flag)
|
29
|
+
end
|
30
|
+
def disable_easy_nils(flag=true)
|
31
|
+
NilClass.disable_easy_nils(flag)
|
32
|
+
end
|
33
|
+
def enable_easy_nils(flag=true)
|
34
|
+
NilClass.disable_easy_nils(!flag)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "EasyNils" do
|
4
|
+
|
5
|
+
describe "method calls on NilClass" do
|
6
|
+
before(:each) do
|
7
|
+
NilClass.disable_easy_nils(false)
|
8
|
+
end
|
9
|
+
it "should return nil" do
|
10
|
+
lambda { nil.my_method?.should == nil }.should_not raise_exception
|
11
|
+
end
|
12
|
+
end
|
13
|
+
describe "method calls on methods returning nil" do
|
14
|
+
before(:each) do
|
15
|
+
NilClass.disable_easy_nils(false)
|
16
|
+
end
|
17
|
+
it "should return nil" do
|
18
|
+
class A1; def nilly; nil; end; end
|
19
|
+
lambda { A1.new.nilly.nil?.should == true }.should_not raise_exception
|
20
|
+
lambda { A1.new.nilly.empty?.should == nil }.should_not raise_exception
|
21
|
+
lambda { A1.new.nilly.blank?.should == nil }.should_not raise_exception
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "methods calls returning nil for comparison operators" do
|
25
|
+
before(:each) do
|
26
|
+
NilClass.disable_easy_nils(false)
|
27
|
+
end
|
28
|
+
it "should compare correctly" do
|
29
|
+
class A2; def nilly; nil; end; end
|
30
|
+
lambda { (A2.new.nilly == 2).should == false }.should_not raise_exception
|
31
|
+
(A2.new.nilly > 2).should be_false
|
32
|
+
(A2.new.nilly <= 2).should be_false
|
33
|
+
(A2.new.nilly == nil).should be_true
|
34
|
+
(A2.new.nilly != nil).should be_false
|
35
|
+
(A2.new.nilly != 5).should be_true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "EasyNils" do
|
4
|
+
describe "disable_easy_nils method" do
|
5
|
+
it "should be called globally" do
|
6
|
+
disable_easy_nils
|
7
|
+
lambda { nil.callthis }.should raise_exception(NoMethodError)
|
8
|
+
enable_easy_nils
|
9
|
+
lambda { nil.callthis }.should_not raise_exception
|
10
|
+
end
|
11
|
+
it "should take a flag parameter" do
|
12
|
+
disable_easy_nils(true)
|
13
|
+
lambda { nil.callthis }.should raise_exception(NoMethodError)
|
14
|
+
disable_easy_nils(false)
|
15
|
+
lambda { nil.callthis }.should_not raise_exception
|
16
|
+
enable_easy_nils(true)
|
17
|
+
lambda { nil.callthis }.should_not raise_exception
|
18
|
+
enable_easy_nils(false)
|
19
|
+
lambda { nil.callthis }.should raise_exception(NoMethodError)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_nils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: 0.0.2
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Jeff Patmon
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-04-30 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: meta_programming
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 0
|
30
|
+
- 6
|
31
|
+
version: 0.0.6
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Simpler nil handling.
|
35
|
+
email: jpatmon@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files: []
|
41
|
+
|
42
|
+
files:
|
43
|
+
- lib/easy_nils.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/easy_nils_spec.rb
|
46
|
+
- spec/enable_disable_spec.rb
|
47
|
+
- spec/spec.opts
|
48
|
+
- init.rb
|
49
|
+
- LICENSE
|
50
|
+
- Rakefile
|
51
|
+
- README.rdoc
|
52
|
+
- .gitignore
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/jeffp/easy_nils/tree/master
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 8
|
69
|
+
- 7
|
70
|
+
version: 1.8.7
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Abridge compound conditional statements to a single expression.
|
85
|
+
test_files:
|
86
|
+
- spec/easy_nils_spec.rb
|
87
|
+
- spec/enable_disable_spec.rb
|