zeke-monkey_patches 0.1.4

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Zeke Sikelianos
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,7 @@
1
+ = monkey_patches
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Zeke Sikelianos. See LICENSE for details.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 4
3
+ :major: 0
4
+ :minor: 1
@@ -0,0 +1,180 @@
1
+ class String
2
+
3
+ # Removes the middle from long strings, replacing with a placeholder
4
+ def ellipsize(options={})
5
+ max = options[:max] || 40
6
+ delimiter = options[:delimiter] || "..."
7
+ return self if self.size <= max
8
+ offset = max/2
9
+ self[0,offset] + delimiter + self[-offset,offset]
10
+ end
11
+
12
+ # Generates a permalink-style string, with odd characters removed, etc.
13
+ def permalinkify
14
+ result = self.to_s
15
+ result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics).
16
+ result.gsub!(/[^\w_ \-]+/i, '') # Remove unwanted chars.
17
+ result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row.
18
+ result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator.
19
+ result.downcase!
20
+ end
21
+
22
+ # Prepends 'http://' to the beginning of non-empty strings that don't already have it.
23
+ def add_http
24
+ return "" if self.blank?
25
+ return "http://#{self}" unless self.starts_with?("http")
26
+ self
27
+ end
28
+
29
+ # Removes presentationally superflous http and/or www text from the beginning of the string
30
+ def remove_http_and_www
31
+ return "" if self.blank?
32
+ return self.split(".").remove_first_element.join(".") if self.starts_with?("www.")
33
+ self.gsub("http://www.", "").gsub("http://", "").gsub("https://www.", "").gsub("https://", "")
34
+ end
35
+
36
+ # Shortens a string, preserving the last word. Truncation can be limited by words or characters
37
+ def truncate_preserving_words(options={})
38
+ end_string = options[:end_string] || "..."
39
+ max_words = options[:max_words] || nil
40
+ if max_words
41
+ words = self.split()
42
+ return self if words.size < max_words
43
+ words = words[0..(max_words-1)]
44
+ words << end_string
45
+ words.join(" ")
46
+ else
47
+ max_chars = options[:max_chars] || 60
48
+ return self if self.size < max_chars
49
+ out = self[0..(max_chars-1)].split(" ")
50
+ out.pop
51
+ out << end_string
52
+ out.join(" ")
53
+ end
54
+ end
55
+
56
+ def replace_wonky_characters_with_ascii
57
+ t = self.to_s
58
+ t.gsub!(/&#8211;/, '-') # en-dash
59
+ t.gsub!(/&#8212;/, '--') # em-dash
60
+ t.gsub!(/&#8230;/, '...') # ellipsis
61
+ t.gsub!(/&#8216;/, "'") # open single quote
62
+ t.gsub!(/&#8217;/, "'") # close single quote
63
+ t.gsub!(/&#8220;/, '"') # open double quote
64
+ t.gsub!(/&#8221;/, '"') # close double quote
65
+
66
+ t.gsub!("\342\200\042", "-") # en-dash
67
+ t.gsub!("\342\200\041", "--") # em-dash
68
+ t.gsub!("\342\200\174", "...") # ellipsis
69
+ t.gsub!("\342\200\176", "'") # single quote
70
+ t.gsub!("\342\200\177", "'") # single quote
71
+ t.gsub!("\342\200\230", "'") # single quote
72
+ t.gsub!("\342\200\231", "'") # single quote
73
+ t.gsub!("\342\200\234", "\"") # Double quote, right
74
+ t.gsub!("\342\200\235", "\"") # Double quote, left
75
+ t.gsub!("\342\200\242", ".")
76
+ t.gsub!("\342\202\254", "&euro;"); # Euro symbol
77
+ t.gsub!(/\S\200\S/, " ") # every other strange character send to the moon
78
+ t.gsub!("\176", "\'") # single quote
79
+ t.gsub!("\177", "\'") # single quote
80
+ t.gsub!("\205", "-") # ISO-Latin1 horizontal elipses (0x85)
81
+ t.gsub!("\221", "\'") # ISO-Latin1 left single-quote
82
+ t.gsub!("\222", "\'") # ISO-Latin1 right single-quote
83
+ t.gsub!("\223", "\"") # ISO-Latin1 left double-quote
84
+ t.gsub!("\224", "\"") # ISO-Latin1 right double-quote
85
+ t.gsub!("\225", "\*") # ISO-Latin1 bullet
86
+ t.gsub!("\226", "-") # ISO-Latin1 en-dash (0x96)
87
+ t.gsub!("\227", "-") # ISO-Latin1 em-dash (0x97)
88
+ t.gsub!("\230", "\'") # single quote
89
+ t.gsub!("\231", "\'") # single quote
90
+ t.gsub!("\233", ">") # ISO-Latin1 single right angle quote
91
+ t.gsub!("\234", "\"") # Double quote
92
+ t.gsub!("\235", "\"") # Double quote
93
+ t.gsub!("\240", " ") # ISO-Latin1 nonbreaking space
94
+ t.gsub!("\246", "\|") # ISO-Latin1 broken vertical bar
95
+ t.gsub!("\255", "") # ISO-Latin1 soft hyphen (0xAD)
96
+ t.gsub!("\264", "\'") # ISO-Latin1 spacing acute
97
+ t.gsub!("\267", "\*") # ISO-Latin1 middle dot (0xB7)
98
+ t
99
+ end
100
+
101
+ # Cleans up MS Word-style text, getting rid of things like em-dashes, smart quotes, etc..
102
+ def replace_wonky_characters_with_entities
103
+ t = self.to_s
104
+ t.gsub!("\342\200\042", "&ndash;") # en-dash
105
+ t.gsub!("\342\200\041", "&mdash;") # em-dash
106
+ t.gsub!("\342\200\174", "&hellip;") # ellipsis
107
+ t.gsub!("\342\200\176", "&lsquo;") # single quote
108
+ t.gsub!("\342\200\177", "&rsquo;") # single quote
109
+ t.gsub!("\342\200\230", "&rsquo;") # single quote
110
+ t.gsub!("\342\200\231", "&rsquo;") # single quote
111
+ t.gsub!("\342\200\234", "&ldquo;") # Double quote, right
112
+ t.gsub!("\342\200\235", "&rdquo;") # Double quote, left
113
+ t.gsub!("\342\200\242", ".")
114
+ t.gsub!("\342\202\254", "&euro;"); # Euro symbol
115
+ t.gsub!(/\S\200\S/, " ") # every other strange character send to the moon
116
+ t.gsub!("\176", "\'") # single quote
117
+ t.gsub!("\177", "\'") # single quote
118
+ t.gsub!("\205", "-") # ISO-Latin1 horizontal elipses (0x85)
119
+ t.gsub!("\221", "\'") # ISO-Latin1 left single-quote
120
+ t.gsub!("\222", "\'") # ISO-Latin1 right single-quote
121
+ t.gsub!("\223", "\"") # ISO-Latin1 left double-quote
122
+ t.gsub!("\224", "\"") # ISO-Latin1 right double-quote
123
+ t.gsub!("\225", "\*") # ISO-Latin1 bullet
124
+ t.gsub!("\226", "-") # ISO-Latin1 en-dash (0x96)
125
+ t.gsub!("\227", "-") # ISO-Latin1 em-dash (0x97)
126
+ t.gsub!("\230", "\'") # single quote
127
+ t.gsub!("\231", "\'") # single quote
128
+ t.gsub!("\233", ">") # ISO-Latin1 single right angle quote
129
+ t.gsub!("\234", "\"") # Double quote
130
+ t.gsub!("\235", "\"") # Double quote
131
+ t.gsub!("\240", " ") # ISO-Latin1 nonbreaking space
132
+ t.gsub!("\246", "\|") # ISO-Latin1 broken vertical bar
133
+ t.gsub!("\255", "") # ISO-Latin1 soft hyphen (0xAD)
134
+ t.gsub!("\264", "\'") # ISO-Latin1 spacing acute
135
+ t.gsub!("\267", "\*") # ISO-Latin1 middle dot (0xB7)
136
+ t
137
+ end
138
+
139
+ unless method_defined? "ends_with?"
140
+ # Snagged from Rails: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html#M000441
141
+ def ends_with?(suffix)
142
+ suffix = suffix.to_s
143
+ self[-suffix.length, suffix.length] == suffix
144
+ end
145
+ end
146
+
147
+ unless method_defined? "starts_with?"
148
+ # Snagged from Rails: http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/StartsEndsWith.html#M000441
149
+ def starts_with?(prefix)
150
+ prefix = prefix.to_s
151
+ self[0, prefix.length] == prefix
152
+ end
153
+ end
154
+
155
+ end
156
+
157
+ class Array
158
+
159
+ # Like Array.shift, but returns the array instead of removed the element.
160
+ def remove_first_element
161
+ self[1..self.size]
162
+ end
163
+
164
+ # Like Array.pop, but returns the array instead of removed the element.
165
+ def remove_last_element
166
+ self[0..self.size-2]
167
+ end
168
+
169
+ end
170
+
171
+ class Object
172
+
173
+ unless method_defined? "blank?"
174
+ # Snagged from Rails: http://api.rubyonrails.org/classes/Object.html#M000265
175
+ def blank?
176
+ respond_to?(:empty?) ? empty? : !self
177
+ end
178
+ end
179
+
180
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe "MonkeyPatches" do
4
+
5
+ it "ellipsizes" do
6
+ "short thing".ellipsize.should == "short thing"
7
+ "0123456789ABCDEFGHIJ".ellipsize(:max => 9).should == "0123...GHIJ"
8
+ "0123456789ABCDEFGHIJ".ellipsize(:max => 10, :delimiter => "|").should == "01234|FGHIJ"
9
+ end
10
+
11
+ it "permalinkifies" do
12
+ "Dog Breath".permalinkify.should == "dog-breath"
13
+ "Shit for @@@ BRAINS!".permalinkify.should == "shit-for-brains"
14
+ " A REal Doozi\"e? \' ".permalinkify.should == "a-real-doozie"
15
+ end
16
+
17
+ it "prepends http only if needed" do
18
+ "".add_http.should == ""
19
+ "dog".add_http.should == "http://dog"
20
+ "http://dog.com".add_http.should == "http://dog.com"
21
+ "https://dog.com".add_http.should == "https://dog.com"
22
+ end
23
+
24
+ it "removes http and www" do
25
+ "http://shitstorm.com".remove_http_and_www.should == "shitstorm.com"
26
+ "http://www.google.com".remove_http_and_www.should == "google.com"
27
+ "http://wwwxyz.com".remove_http_and_www.should == "wwwxyz.com"
28
+ "www.abc.com".remove_http_and_www.should == "abc.com"
29
+ "https://secure.com".remove_http_and_www.should == "secure.com"
30
+ "https://www.dubsecure.com".remove_http_and_www.should == "dubsecure.com"
31
+ end
32
+
33
+ it "truncates by words" do
34
+ #012345678901234567890123456789012345678901234567890123456789
35
+ "this is short. should be fine.".truncate_preserving_words.should == "this is short. should be fine."
36
+ "this is longer. will cut if we leave the default max_chars in place".truncate_preserving_words.should == "this is longer. will cut if we leave the default max_chars ..."
37
+ "this will get cut".truncate_preserving_words(:max_chars => 15, :end_string => "..").should == "this will get .."
38
+ "this doesn't have too many words".truncate_preserving_words(:max_words => 10).should == "this doesn't have too many words"
39
+ "this has too many words".truncate_preserving_words(:max_words => 3).should == "this has too ..."
40
+ end
41
+
42
+ it "replaces wonky characters with ascii" do
43
+ "\“Ulysses\”".replace_wonky_characters_with_ascii.should == "\"Ulysses\""
44
+ "We ‘are’ single".replace_wonky_characters_with_ascii.should == "We 'are' single"
45
+ "We ‘are’ single".replace_wonky_characters_with_ascii.should == "We 'are' single"
46
+ end
47
+
48
+ # Array specs
49
+
50
+ it "removes first element" do
51
+ %w(1 2 3).remove_first_element.should == %w(2 3)
52
+ end
53
+
54
+ it "removes last element" do
55
+ %w(1 2 3).remove_last_element.should == %w(1 2)
56
+ end
57
+
58
+
59
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'monkey_patches'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zeke-monkey_patches
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Zeke Sikelianos
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: zeke@sikelianos.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - LICENSE
25
+ files:
26
+ - README.rdoc
27
+ - VERSION.yml
28
+ - lib/monkey_patches.rb
29
+ - spec/monkey_patches_spec.rb
30
+ - spec/spec_helper.rb
31
+ - LICENSE
32
+ has_rdoc: true
33
+ homepage: http://github.com/zeke/monkey_patches
34
+ post_install_message:
35
+ rdoc_options:
36
+ - --inline-source
37
+ - --charset=UTF-8
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ version:
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.2.0
56
+ signing_key:
57
+ specification_version: 2
58
+ summary: A handly collection of helper methods, primarily for String and Array
59
+ test_files: []
60
+