nutella 0.6 → 0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/nutella/core_ext/integer/format.rb +3 -1
- data/lib/nutella/core_ext/object/aliases.rb +3 -0
- data/lib/nutella/core_ext/object/blank.rb +42 -0
- data/lib/nutella/core_ext/object.rb +1 -0
- data/lib/nutella/version.rb +1 -1
- data/spec/nutella/core_ext/integer_spec.rb +1 -1
- data/spec/nutella/core_ext/object_spec.rb +74 -0
- metadata +7 -2
@@ -1,10 +1,12 @@
|
|
1
1
|
class Integer
|
2
|
-
# Returns a string with the ordinal form of
|
2
|
+
# Returns a string with the ordinal form of the integer.
|
3
3
|
#
|
4
4
|
# 1.ordinalize # => "1st"
|
5
5
|
# 2.ordinalize # => "2nd"
|
6
6
|
# 9.ordinalize # => "9th"
|
7
7
|
# 43.ordinalize # => "43rd"
|
8
|
+
#
|
9
|
+
# @return [String] the ordinal form of the integer
|
8
10
|
def ordinalize
|
9
11
|
suffixes = { 1 => "st", 2 => "nd", 3 => "rd" }
|
10
12
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class Object
|
2
|
+
# Returns true if the object is false, empty, or a whitespace string.
|
3
|
+
#
|
4
|
+
# nil.blank? # => true
|
5
|
+
# false.blank? # => true
|
6
|
+
# 4.blank? # => false
|
7
|
+
#
|
8
|
+
# @example Compare with and without usage of Object#blank?
|
9
|
+
# # Without:
|
10
|
+
# if str.nil? || str.empty?
|
11
|
+
# # With:
|
12
|
+
# if str.blank?
|
13
|
+
#
|
14
|
+
# @return [Boolean] whether or not the object is blank
|
15
|
+
def blank?
|
16
|
+
respond_to?(:empty?) ? empty? : !self
|
17
|
+
end
|
18
|
+
|
19
|
+
# The inverse of Object#blank?.
|
20
|
+
#
|
21
|
+
# @return [Boolean] whether or not the object is not blank
|
22
|
+
def present?
|
23
|
+
!blank?
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the object if it is present, returns <tt>nil</tt> if the object is
|
27
|
+
# blank.
|
28
|
+
#
|
29
|
+
# "str".presence # => "str"
|
30
|
+
# "".presence # => nil
|
31
|
+
#
|
32
|
+
# @return [Object, NilClass] the object if present, otherwise nil
|
33
|
+
def presence
|
34
|
+
self if present?
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class String #:nodoc:
|
39
|
+
def blank?
|
40
|
+
self !~ /[^\s]/
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Dir[File.expand_path("../object/**/*.rb", __FILE__)].each { |f| require f }
|
data/lib/nutella/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe Integer do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "#ordinalize" do
|
11
|
-
it "should return the
|
11
|
+
it "should return the ordinal form of the integer" do
|
12
12
|
NUMBER_FORMATS.each do |cardinal, ordinal|
|
13
13
|
cardinal.ordinalize.should == ordinal
|
14
14
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "nutella/core_ext/object"
|
3
|
+
|
4
|
+
describe Object do
|
5
|
+
describe "aliases" do
|
6
|
+
test_alias Object, :is_an?, :is_a?
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#blank?" do
|
10
|
+
it "should be blank when nil" do
|
11
|
+
nil.blank?.should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be blank when false" do
|
15
|
+
false.blank?.should be_true
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not be blank when true" do
|
19
|
+
true.blank?.should be_false
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not be blank when numeric" do
|
23
|
+
[0, 1].each { |n| n.blank?.should be_false }
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when a string" do
|
27
|
+
it "should be blank if the string is empty" do
|
28
|
+
"".blank?.should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be blank if only whitespace" do
|
32
|
+
[" ", "\n \t"].each { |str| str.blank?.should be_true }
|
33
|
+
" something here ".blank?.should be_false
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not be blank if the string has content" do
|
37
|
+
"string".blank?.should be_false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when a collection" do
|
42
|
+
it "should be blank if the collection is empty" do
|
43
|
+
[[], {}].each { |collection| collection.blank?.should be_true }
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should not be blank if there are elements in the collection" do
|
47
|
+
[[1, 2, 3], { 1 => 2, 3 => 4 }].each do |collection|
|
48
|
+
collection.blank?.should be_false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "#present?" do
|
55
|
+
it "should be the inverse of #blank?" do
|
56
|
+
[0, 1].each { |n| n.present?.should be_true }
|
57
|
+
[[], {}].each { |collection| collection.present?.should be_false }
|
58
|
+
["", " "].each { |str| str.present?.should be_false }
|
59
|
+
["str", " str "].each { |str| str.present?.should be_true }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#presence" do
|
64
|
+
it "should return the object if the object is present" do
|
65
|
+
1.presence.should == 1
|
66
|
+
"str".presence.should == "str"
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should return nil if the object is not present" do
|
70
|
+
"".presence.should be_nil
|
71
|
+
false.presence.should be_nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nutella
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.7'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -118,13 +118,17 @@ files:
|
|
118
118
|
- lib/nutella/input.rb
|
119
119
|
- lib/nutella/core_ext/integer/multiples.rb
|
120
120
|
- lib/nutella/core_ext/integer/format.rb
|
121
|
+
- lib/nutella/core_ext/object.rb
|
121
122
|
- lib/nutella/core_ext/string.rb
|
122
123
|
- lib/nutella/core_ext/integer.rb
|
123
124
|
- lib/nutella/core_ext/hash.rb
|
125
|
+
- lib/nutella/core_ext/object/blank.rb
|
126
|
+
- lib/nutella/core_ext/object/aliases.rb
|
124
127
|
- lib/nutella/core_ext/enumerable.rb
|
125
128
|
- lib/nutella/core_ext.rb
|
126
129
|
- spec/support/alias.rb
|
127
130
|
- spec/support/number_formats.rb
|
131
|
+
- spec/nutella/core_ext/object_spec.rb
|
128
132
|
- spec/nutella/core_ext/hash_spec.rb
|
129
133
|
- spec/nutella/core_ext/enumerable_spec.rb
|
130
134
|
- spec/nutella/core_ext/integer_spec.rb
|
@@ -156,7 +160,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
160
|
version: '0'
|
157
161
|
segments:
|
158
162
|
- 0
|
159
|
-
hash:
|
163
|
+
hash: -3181334132306800521
|
160
164
|
requirements: []
|
161
165
|
rubyforge_project:
|
162
166
|
rubygems_version: 1.8.24
|
@@ -166,6 +170,7 @@ summary: Spread some Nutella on Ruby to sweeten up its functionality.
|
|
166
170
|
test_files:
|
167
171
|
- spec/support/alias.rb
|
168
172
|
- spec/support/number_formats.rb
|
173
|
+
- spec/nutella/core_ext/object_spec.rb
|
169
174
|
- spec/nutella/core_ext/hash_spec.rb
|
170
175
|
- spec/nutella/core_ext/enumerable_spec.rb
|
171
176
|
- spec/nutella/core_ext/integer_spec.rb
|