zeke-monkey_patches 0.1.11 → 0.1.12
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/VERSION.yml +1 -1
- data/lib/monkey_patches.rb +39 -21
- data/spec/monkey_patches_spec.rb +16 -0
- metadata +1 -1
data/VERSION.yml
CHANGED
data/lib/monkey_patches.rb
CHANGED
@@ -1,3 +1,25 @@
|
|
1
|
+
class Object
|
2
|
+
|
3
|
+
unless method_defined? "blank?"
|
4
|
+
# Snagged from Rails: http://api.rubyonrails.org/classes/Object.html#M000265
|
5
|
+
def blank?
|
6
|
+
respond_to?(:empty?) ? empty? : !self
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
# @person ? @person.name : nil
|
11
|
+
# vs
|
12
|
+
# @person.try(:name)
|
13
|
+
# Snagged from http://ozmm.org/posts/try.html; later incorporated into Rails 2.3
|
14
|
+
unless method_defined? "try"
|
15
|
+
def try(method)
|
16
|
+
send method if respond_to? method
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
end
|
22
|
+
|
1
23
|
class String
|
2
24
|
|
3
25
|
# Pollute the space between every letter in a string,
|
@@ -78,6 +100,19 @@ class String
|
|
78
100
|
out.join(" ")
|
79
101
|
end
|
80
102
|
end
|
103
|
+
|
104
|
+
def domain
|
105
|
+
url = self.dup
|
106
|
+
url=~(/^(?:\w+:\/\/)?([^\/?]+)(?:\/|\?|$)/) ? $1 : nil
|
107
|
+
end
|
108
|
+
|
109
|
+
def domain_without_www
|
110
|
+
self.domain.remove_http_and_www
|
111
|
+
end
|
112
|
+
|
113
|
+
def remove_whitespace
|
114
|
+
self.gnix("\t").split(" ").remove_blanks.join(" ")
|
115
|
+
end
|
81
116
|
|
82
117
|
def replace_wonky_characters_with_ascii
|
83
118
|
t = self.to_s
|
@@ -181,6 +216,10 @@ class String
|
|
181
216
|
end
|
182
217
|
|
183
218
|
class Array
|
219
|
+
|
220
|
+
def remove_blanks
|
221
|
+
self.reject{ |e| e.blank? }
|
222
|
+
end
|
184
223
|
|
185
224
|
# Like Array.shift, but returns the array instead of removed the element.
|
186
225
|
def remove_first_element
|
@@ -194,24 +233,3 @@ class Array
|
|
194
233
|
|
195
234
|
end
|
196
235
|
|
197
|
-
class Object
|
198
|
-
|
199
|
-
unless method_defined? "blank?"
|
200
|
-
# Snagged from Rails: http://api.rubyonrails.org/classes/Object.html#M000265
|
201
|
-
def blank?
|
202
|
-
respond_to?(:empty?) ? empty? : !self
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
|
-
# @person ? @person.name : nil
|
207
|
-
# vs
|
208
|
-
# @person.try(:name)
|
209
|
-
# Snagged from http://ozmm.org/posts/try.html; later incorporated into Rails 2.3
|
210
|
-
unless method_defined? "try"
|
211
|
-
def try(method)
|
212
|
-
send method if respond_to? method
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
|
217
|
-
end
|
data/spec/monkey_patches_spec.rb
CHANGED
@@ -71,7 +71,23 @@ describe "MonkeyPatches" do
|
|
71
71
|
s.pollute("-").sanitize("-").should == s
|
72
72
|
end
|
73
73
|
|
74
|
+
it "extracts domain from a string" do
|
75
|
+
"http://www.google.com".domain.should == "www.google.com"
|
76
|
+
"http://www.google.com".domain_without_www.should == "google.com"
|
77
|
+
"not a url".domain.should == "not a url"
|
78
|
+
end
|
79
|
+
|
80
|
+
it "removes whitespace" do
|
81
|
+
"this is a test".remove_whitespace.should == "this is a test"
|
82
|
+
" this \t is also a test ".remove_whitespace.should == "this is also a test"
|
83
|
+
end
|
84
|
+
|
74
85
|
# Array specs
|
86
|
+
|
87
|
+
it "removes blank elements from an array" do
|
88
|
+
"this is a test".split(" ").remove_blanks.should == %w(this is a test)
|
89
|
+
["this", "", "that", nil].remove_blanks.should == %w(this that)
|
90
|
+
end
|
75
91
|
|
76
92
|
it "removes first element" do
|
77
93
|
%w(1 2 3).remove_first_element.should == %w(2 3)
|