if_blank 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/README.md +7 -3
- data/lib/if_blank.rb +7 -1
- data/lib/if_blank/version.rb +1 -1
- data/spec/if_blank_spec.rb +105 -0
- data/spec/spec_helper.rb +12 -0
- metadata +7 -3
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# IfBlank
|
2
2
|
|
3
|
-
|
3
|
+
Return a value if the object is empty
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -18,7 +18,11 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
### Block Usage
|
22
|
+
`obj.if_blank? { do_something }`
|
23
|
+
|
24
|
+
### Value Usage
|
25
|
+
`obj.if_blank?("some string")`
|
22
26
|
|
23
27
|
## Contributing
|
24
28
|
|
@@ -26,4 +30,4 @@ TODO: Write usage instructions here
|
|
26
30
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
31
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
32
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
-
5. Create new Pull Request
|
33
|
+
5. Create new Pull Request
|
data/lib/if_blank.rb
CHANGED
@@ -7,7 +7,7 @@ class Object
|
|
7
7
|
# its return value to be the result of a passed
|
8
8
|
# in block, or else a replacement string
|
9
9
|
# Converts self to a string.
|
10
|
-
def if_blank(replacement = "")
|
10
|
+
def if_blank?(replacement = "")
|
11
11
|
return self unless self.tap do |obj|
|
12
12
|
obj.strip! if obj.respond_to?(:strip!)
|
13
13
|
end.empty?
|
@@ -15,4 +15,10 @@ class Object
|
|
15
15
|
block_given? ? yield : replacement
|
16
16
|
end
|
17
17
|
|
18
|
+
end
|
19
|
+
|
20
|
+
class NilClass
|
21
|
+
def empty?
|
22
|
+
true
|
23
|
+
end
|
18
24
|
end
|
data/lib/if_blank/version.rb
CHANGED
@@ -0,0 +1,105 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
|
5
|
+
context "replacement" do
|
6
|
+
|
7
|
+
it "is present" do
|
8
|
+
"value".if_blank?("hi").should == "value"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "is whitespace" do
|
12
|
+
" ".if_blank?("hi").should == "hi"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
context "block" do
|
18
|
+
|
19
|
+
it "is present" do
|
20
|
+
"value".if_blank? { "hi" }.should == "value"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "is whitespace" do
|
24
|
+
" ".if_blank? { "hi" }.should == "hi"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe Array do
|
32
|
+
|
33
|
+
context "replacement" do
|
34
|
+
|
35
|
+
it "is present" do
|
36
|
+
["value", "value2"].if_blank?("hi").should == ["value", "value2"]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "is whitespace" do
|
40
|
+
[].if_blank?("hi").should == "hi"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context "block" do
|
46
|
+
|
47
|
+
it "is present" do
|
48
|
+
["value", "value2"].if_blank? { "hi" }.should == ["value", "value2"]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "is whitespace" do
|
52
|
+
[].if_blank? { "hi" }.should == "hi"
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe Hash do
|
60
|
+
|
61
|
+
context "replacement" do
|
62
|
+
|
63
|
+
it "is present" do
|
64
|
+
{:key => "value"}.if_blank?("hi").should == {:key => "value"}
|
65
|
+
end
|
66
|
+
|
67
|
+
it "is whitespace" do
|
68
|
+
{}.if_blank?("hi").should == "hi"
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
context "block" do
|
74
|
+
|
75
|
+
it "is present" do
|
76
|
+
{:key => "value"}.if_blank? { "hi" }.should == {:key => "value"}
|
77
|
+
end
|
78
|
+
|
79
|
+
it "is whitespace" do
|
80
|
+
{}.if_blank? { "hi" }.should == "hi"
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
describe NilClass do
|
88
|
+
|
89
|
+
context "replacement" do
|
90
|
+
|
91
|
+
it "is nil" do
|
92
|
+
nil.if_blank?("hi").should == "hi"
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
context "block" do
|
98
|
+
|
99
|
+
it "is nil" do
|
100
|
+
nil.if_blank? { "hi" }.should == "hi"
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler/setup"
|
3
|
+
|
4
|
+
require "if_blank"
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
# only run "focused" test, see http://railscasts.com/episodes/285-spork
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
# run all tests by default, but leave setup for filters
|
10
|
+
config.filter_run :focus => true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: if_blank
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: If a string is empty return the passed in parameter (or block if given)
|
15
15
|
email:
|
@@ -26,6 +26,8 @@ files:
|
|
26
26
|
- if_blank.gemspec
|
27
27
|
- lib/if_blank.rb
|
28
28
|
- lib/if_blank/version.rb
|
29
|
+
- spec/if_blank_spec.rb
|
30
|
+
- spec/spec_helper.rb
|
29
31
|
homepage: https://github.com/ready4god2513/if_blank
|
30
32
|
licenses: []
|
31
33
|
post_install_message:
|
@@ -50,5 +52,7 @@ rubygems_version: 1.8.24
|
|
50
52
|
signing_key:
|
51
53
|
specification_version: 3
|
52
54
|
summary: If a string is empty return the passed in parameter (or block if given)
|
53
|
-
test_files:
|
55
|
+
test_files:
|
56
|
+
- spec/if_blank_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
54
58
|
has_rdoc:
|