naught_check 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +32 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/naught_check.rb +55 -0
- data/test/helper.rb +11 -0
- data/test/test_naught_check.rb +104 -0
- metadata +74 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Sean Miller
|
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,32 @@
|
|
1
|
+
= naught_check
|
2
|
+
|
3
|
+
== Why?
|
4
|
+
|
5
|
+
Got tired of string.empty? checks in a project blowing up on nil strings.
|
6
|
+
|
7
|
+
== Usage:
|
8
|
+
|
9
|
+
require 'naught_check'
|
10
|
+
include Naught
|
11
|
+
|
12
|
+
nil.naught? => true
|
13
|
+
"".naught? => true
|
14
|
+
" ".naught? => true
|
15
|
+
[].naught? => true
|
16
|
+
{}.naught? => true
|
17
|
+
0.naught? => true
|
18
|
+
0.00.naught? => true
|
19
|
+
|
20
|
+
== Compared to ActiveSupport .blank?
|
21
|
+
|
22
|
+
In a Rails project, ActiveSupport .blank? would do almost the same thing,
|
23
|
+
except for numbers:
|
24
|
+
|
25
|
+
0.blank? => false
|
26
|
+
0.naught? => true
|
27
|
+
|
28
|
+
It's like ActiveSupport .blank? combined with a nil-safe Numeric .zero?
|
29
|
+
|
30
|
+
== Copyright
|
31
|
+
|
32
|
+
Copyright (c) 2010 Sean Miller. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "naught_check"
|
8
|
+
gem.summary = %Q{Adds .naught? method: tests if value is nil, .blank?, .zero?}
|
9
|
+
gem.description = %Q{Usage: require 'naught_check'; include Naught}
|
10
|
+
gem.email = "sean@seanmiller.ca"
|
11
|
+
gem.homepage = "http://github.com/smiller/naught_check"
|
12
|
+
gem.authors = ["Sean Miller"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "naught_check #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
data/lib/naught_check.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module Naught
|
2
|
+
|
3
|
+
module InstanceMethods
|
4
|
+
# Tests if a value if nil, blank, or zero
|
5
|
+
#
|
6
|
+
# nil.naught? => true
|
7
|
+
# "".naught? => true
|
8
|
+
# " ".naught? => true
|
9
|
+
# [].naught? => true
|
10
|
+
# {}.naught? => true
|
11
|
+
# 0.naught? => true
|
12
|
+
# 0.00.naught? => true
|
13
|
+
def naught?
|
14
|
+
if self == nil
|
15
|
+
true
|
16
|
+
elsif self.kind_of? Array
|
17
|
+
self.size == 0
|
18
|
+
elsif self.kind_of? Hash
|
19
|
+
self.keys.size == 0
|
20
|
+
elsif self.kind_of? Numeric
|
21
|
+
self.zero?
|
22
|
+
elsif self.kind_of? String
|
23
|
+
self == "" || (/^\s*$/.match(self) != nil)
|
24
|
+
else
|
25
|
+
false
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# aliases .naught?
|
30
|
+
def nought?; naught?; end
|
31
|
+
|
32
|
+
# aliases .naught?
|
33
|
+
def nowt?; naught?; end
|
34
|
+
|
35
|
+
# aliases .naught?
|
36
|
+
def vacant?; naught?; end
|
37
|
+
|
38
|
+
# inverts naught?
|
39
|
+
def aught?; !naught?; end
|
40
|
+
|
41
|
+
# aliases .aught?
|
42
|
+
def ought?; aught?; end
|
43
|
+
|
44
|
+
# aliases .aught?
|
45
|
+
def owt?; aught?; end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.included(base)
|
50
|
+
base.class_eval do
|
51
|
+
include InstanceMethods
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
class TestNaughtCheck < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "nil case" do
|
6
|
+
should "be naught" do
|
7
|
+
assert_equal true, nil.naught?
|
8
|
+
assert_equal true, nil.nought?
|
9
|
+
assert_equal true, nil.nowt?
|
10
|
+
assert_equal true, nil.vacant?
|
11
|
+
assert_equal false, nil.aught?
|
12
|
+
assert_equal false, nil.ought?
|
13
|
+
assert_equal false, nil.owt?
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "string case" do
|
19
|
+
should "zero_length be naught" do
|
20
|
+
assert_equal true, "".naught?
|
21
|
+
assert_equal true, "".nought?
|
22
|
+
assert_equal true, "".nowt?
|
23
|
+
assert_equal true, "".vacant?
|
24
|
+
assert_equal false, "".aught?
|
25
|
+
assert_equal false, "".ought?
|
26
|
+
assert_equal false, "".owt?
|
27
|
+
end
|
28
|
+
|
29
|
+
should "white_space be naught" do
|
30
|
+
assert_equal true, " ".naught?
|
31
|
+
assert_equal true, " ".nought?
|
32
|
+
assert_equal true, " ".nowt?
|
33
|
+
assert_equal true, " ".vacant?
|
34
|
+
assert_equal false, " ".aught?
|
35
|
+
assert_equal false, " ".ought?
|
36
|
+
assert_equal false, " ".owt?
|
37
|
+
end
|
38
|
+
|
39
|
+
should "not_empty not be naught" do
|
40
|
+
assert_equal false, "gilgamesh".naught?
|
41
|
+
assert_equal false, "gilgamesh".nought?
|
42
|
+
assert_equal false, "gilgamesh".nowt?
|
43
|
+
assert_equal false, "gilgamesh".vacant?
|
44
|
+
assert_equal true, "gilgamesh".aught?
|
45
|
+
assert_equal true, "gilgamesh".ought?
|
46
|
+
assert_equal true, "gilgamesh".owt?
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "array case" do
|
51
|
+
should "zero_length_array be naught" do
|
52
|
+
assert_equal true, [].naught?
|
53
|
+
assert_equal false, [].aught?
|
54
|
+
end
|
55
|
+
|
56
|
+
should "not_empty_array not be naught" do
|
57
|
+
assert_equal false, [1, 2, 3].naught?
|
58
|
+
assert_equal true, [1, 2, 3].aught?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context "hash case" do
|
63
|
+
should "hash_with_no_keys be naught" do
|
64
|
+
assert_equal true, {}.naught?
|
65
|
+
assert_equal false, {}.aught?
|
66
|
+
end
|
67
|
+
|
68
|
+
should "hash_with_keys not be naught" do
|
69
|
+
assert_equal false, {:key => :value}.naught?
|
70
|
+
assert_equal true, {:key => :value}.aught?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context "integer case" do
|
75
|
+
should "0 be naught" do
|
76
|
+
assert_equal true, 0.naught?
|
77
|
+
assert_equal false, 0.aught?
|
78
|
+
end
|
79
|
+
|
80
|
+
should "1 not be naught" do
|
81
|
+
assert_equal false, 1.naught?
|
82
|
+
assert_equal true, 1.aught?
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "float case" do
|
87
|
+
should "0.00 be naught" do
|
88
|
+
assert_equal true, 0.00.naught?
|
89
|
+
assert_equal false, 0.00.aught?
|
90
|
+
end
|
91
|
+
|
92
|
+
should "0.01 not be naught" do
|
93
|
+
assert_equal false, 0.01.naught?
|
94
|
+
assert_equal true, 0.01.aught?
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "unknown object case" do
|
99
|
+
should "not be naught" do
|
100
|
+
assert_equal false, Class.new.naught?
|
101
|
+
assert_equal true, Class.new.aught?
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: naught_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Miller
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-02-06 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: "Usage: require 'naught_check'; include Naught"
|
26
|
+
email: sean@seanmiller.ca
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/naught_check.rb
|
42
|
+
- test/helper.rb
|
43
|
+
- test/test_naught_check.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/smiller/naught_check
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options:
|
50
|
+
- --charset=UTF-8
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.5
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: "Adds .naught? method: tests if value is nil, .blank?, .zero?"
|
72
|
+
test_files:
|
73
|
+
- test/helper.rb
|
74
|
+
- test/test_naught_check.rb
|