da_huangs_ruby_extensions 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/array_ext.rb +28 -28
- data/lib/da_huangs_ruby_extensions.rb +8 -0
- data/lib/hash_ext.rb +13 -19
- data/lib/nil_class_ext.rb +8 -6
- data/lib/string_ext.rb +34 -31
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
data/lib/array_ext.rb
CHANGED
@@ -1,37 +1,37 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module DaHuang
|
2
|
+
module ArrayExt
|
3
|
+
def to_hash
|
4
|
+
map { |e| yield e }.inject({}) { |carry, e| carry.merge! e }
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
def strip_each
|
8
|
+
self.map{|t| t.strip}
|
9
|
+
end
|
10
|
+
def strip_each!
|
11
|
+
self.each{|t| t.strip!}
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
def to_sentence
|
15
|
+
i = size
|
16
|
+
if size<=2
|
17
|
+
self.join(" and ")
|
18
|
+
else
|
19
|
+
[self[0..size-2].join(", "), self[size-1]].join(" and ")
|
20
|
+
end
|
19
21
|
end
|
20
|
-
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
def map_with_index
|
24
|
+
result = []
|
25
|
+
self.each_with_index do |elt, idx|
|
26
|
+
result << yield(elt, idx)
|
27
|
+
end
|
28
|
+
result
|
26
29
|
end
|
27
|
-
result
|
28
|
-
end
|
29
|
-
def plus_if(obj)
|
30
|
-
self << obj unless include?(obj) || obj.nil?
|
31
|
-
end
|
32
30
|
|
33
|
-
|
34
|
-
|
31
|
+
def strip_all
|
32
|
+
map!{|t| t.strip}
|
33
|
+
end
|
35
34
|
end
|
35
|
+
|
36
36
|
end
|
37
37
|
|
@@ -1,5 +1,13 @@
|
|
1
|
+
|
1
2
|
require 'array_ext'
|
3
|
+
Array.send :include, DaHuang::ArrayExt
|
4
|
+
|
2
5
|
require 'hash_ext'
|
6
|
+
Hash.send :include, DaHuang::HashExt
|
7
|
+
|
3
8
|
require 'nil_class_ext'
|
9
|
+
NilClass.send :include, DaHuang::NilClassExt
|
10
|
+
|
4
11
|
require 'string_ext'
|
12
|
+
String.send :include, DaHuang::StringExt
|
5
13
|
|
data/lib/hash_ext.rb
CHANGED
@@ -1,24 +1,18 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
end
|
7
|
-
|
8
|
-
def << (hash)
|
9
|
-
hash.each{|a,b| self[a]=b}
|
10
|
-
end
|
1
|
+
module DaHuang
|
2
|
+
module HashExt
|
3
|
+
def add(hash)
|
4
|
+
hash.each{|a,b| self[a]=b}
|
5
|
+
end
|
11
6
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
7
|
+
def map_ary(&block)
|
8
|
+
val = []
|
9
|
+
self.each{|a,b| val.add block.call(a,b)}
|
10
|
+
val
|
11
|
+
end
|
17
12
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
val
|
13
|
+
def reject_keys(ary)
|
14
|
+
self.reject{|a,b| ary.include?(a)}
|
15
|
+
end
|
22
16
|
end
|
23
17
|
end
|
24
18
|
|
data/lib/nil_class_ext.rb
CHANGED
data/lib/string_ext.rb
CHANGED
@@ -1,39 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
module DaHuang
|
2
|
+
module StringExt
|
3
|
+
def normalize
|
4
|
+
ActiveSupport::Multibyte::Chars.new(self).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').to_s
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def is_numeric?
|
8
|
+
/^\d+$/.match(self) ? true : false
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def to_search_string
|
12
|
+
a = gsub(/[-_+]/, " ").gsub(/%20/, " ").downcase
|
13
|
+
a.index("%") ? a : "%#{a}"
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def to_url_string
|
17
|
+
titleize.gsub(" ", "_")
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
20
|
+
# Convert a string to a format suitable for a URL without ever using escaped characters.
|
21
|
+
# It calls strip, removeaccents, downcase (optional) then removes the spaces (optional)
|
22
|
+
# and finally removes any characters matching the default regexp (/[^-_A-Za-z0-9]/).
|
23
|
+
#
|
24
|
+
# Options
|
25
|
+
#
|
26
|
+
# * :downcase => call downcase on the string (defaults to true)
|
27
|
+
# * :convert_spaces => Convert space to underscore (defaults to false)
|
28
|
+
# * :regexp => The regexp matching characters that will be converting to an empty string (defaults to /[^-_A-Za-z0-9]/)
|
29
|
+
def urlize(options = {})
|
30
|
+
options[:downcase] ||= true
|
31
|
+
options[:convert_spaces] ||= false
|
32
|
+
options[:regexp] ||= /[^-_A-Za-z0-9]/
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
str = self.strip.normalize
|
35
|
+
str.downcase! if options[:downcase]
|
36
|
+
str.gsub!(/\ /,'_') if options[:convert_spaces]
|
37
|
+
str.gsub(options[:regexp], '')
|
38
|
+
end
|
37
39
|
end
|
40
|
+
|
38
41
|
end
|
39
42
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: da_huangs_ruby_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tyler Gannon
|