vidibus-core_extensions 0.3.5 → 0.3.6
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/README.rdoc +8 -8
- data/VERSION +1 -1
- data/lib/vidibus/core_extensions/array.rb +57 -61
- data/lib/vidibus/core_extensions/file_utils.rb +24 -0
- data/lib/vidibus/core_extensions/hash.rb +90 -73
- data/lib/vidibus/core_extensions/object.rb +11 -15
- data/lib/vidibus/core_extensions/string.rb +86 -60
- data/lib/vidibus/core_extensions.rb +1 -6
- data/spec/spec_helper.rb +1 -1
- data/spec/vidibus/core_extensions/array_spec.rb +10 -10
- data/spec/vidibus/core_extensions/file_utils_spec.rb +33 -0
- data/spec/vidibus/core_extensions/hash_spec.rb +60 -32
- data/spec/vidibus/core_extensions/object_spec.rb +1 -1
- data/spec/vidibus/core_extensions/string_spec.rb +25 -13
- data/vidibus-core_extensions.gemspec +5 -2
- metadata +7 -4
data/README.rdoc
CHANGED
@@ -19,9 +19,9 @@ Then call bundle install on your console.
|
|
19
19
|
== Array
|
20
20
|
|
21
21
|
=== Array#flatten_once
|
22
|
-
|
22
|
+
|
23
23
|
Flattens first level of an array. Example:
|
24
|
-
|
24
|
+
|
25
25
|
[1, [2, [3]]].flatten_once # => [1, 2, [3]]
|
26
26
|
|
27
27
|
|
@@ -49,22 +49,22 @@ Examples:
|
|
49
49
|
|
50
50
|
Returns URL-encoded string of uri params. Examples:
|
51
51
|
|
52
|
-
{
|
53
|
-
{
|
52
|
+
{:some => :value, :another => "speciál"}.to_uri # => "some=value&another=speci%C3%A1l"
|
53
|
+
{:some => {:nested => :thing}}.to_uri # => "some=[nested=thing]"
|
54
54
|
|
55
55
|
|
56
56
|
=== Hash#only
|
57
57
|
|
58
58
|
Returns a copy of self including only the given keys. Example:
|
59
59
|
|
60
|
-
{
|
60
|
+
{:name => "Rodrigo", :age => 21}.only(:name) # => {:name => "Rodrigo"}
|
61
61
|
|
62
62
|
|
63
63
|
=== Hash#except
|
64
64
|
|
65
65
|
Returns a copy of self including all but the given keys. Example:
|
66
66
|
|
67
|
-
{
|
67
|
+
{:name => "Rodrigo", :age = 21}.except(:name) # => {:age => 21}
|
68
68
|
|
69
69
|
|
70
70
|
== String
|
@@ -72,7 +72,7 @@ Returns a copy of self including all but the given keys. Example:
|
|
72
72
|
=== String#latinize
|
73
73
|
|
74
74
|
Returns a string without exotic chars. Examples:
|
75
|
-
|
75
|
+
|
76
76
|
"Hola señor, ¿cómo está?".latinize # => "Hola senor, como esta?"
|
77
77
|
"Ähre, wem Ähre gebührt.".latinize # => "AEhre, wem AEhre gebuehrt."
|
78
78
|
|
@@ -80,7 +80,7 @@ Returns a string without exotic chars. Examples:
|
|
80
80
|
=== String#permalink
|
81
81
|
|
82
82
|
Returns a string that may be used as permalink. Example:
|
83
|
-
|
83
|
+
|
84
84
|
"Hola señor, ¿cómo está?".permalink # => "hola-senor-como-esta"
|
85
85
|
|
86
86
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.6
|
@@ -1,67 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
else
|
18
|
-
v << e
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# Merges given array with current one.
|
24
|
-
#
|
25
|
-
# Will perform insertion of new items by three rules:
|
26
|
-
# 1. If the item's predecessor is present, insert item after it.
|
27
|
-
# 2. If the item's follower is present, insert item before it.
|
28
|
-
# 3. Insert item at end of list.
|
29
|
-
#
|
30
|
-
# Examples:
|
31
|
-
#
|
32
|
-
# [].merge([1, 2]) # => [1, 2]
|
33
|
-
# ['a'].merge([1, 2]) # => ['a', 1, 2]
|
34
|
-
# [1, 'a'].merge([1, 2]) # => [1, 2, 'a']
|
35
|
-
# [1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
|
36
|
-
#
|
37
|
-
def merge(array)
|
38
|
-
for value in array
|
39
|
-
next if include?(value)
|
40
|
-
if found = merging_predecessor(value, array)
|
41
|
-
position = index(found) + 1
|
42
|
-
elsif found = merging_follower(value, array)
|
43
|
-
position = index(found)
|
44
|
-
end
|
45
|
-
insert(position || length, value)
|
46
|
-
end
|
47
|
-
self
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
# Returns predecessor of given needle from haystack.
|
53
|
-
# Helper method for #merge
|
54
|
-
def merging_predecessor(needle, haystack)
|
55
|
-
list = haystack[0..haystack.index(needle)].reverse
|
56
|
-
(list & self).first
|
1
|
+
class Array
|
2
|
+
|
3
|
+
# Flattens first level.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# [1, [2, [3]]].flatten_once # => [1, 2, [3]]
|
8
|
+
#
|
9
|
+
# Inspired by: http://snippets.dzone.com/posts/show/302#comment-1417
|
10
|
+
#
|
11
|
+
def flatten_once
|
12
|
+
inject([]) do |v,e|
|
13
|
+
if e.is_a?(Array)
|
14
|
+
v.concat(e)
|
15
|
+
else
|
16
|
+
v << e
|
57
17
|
end
|
18
|
+
end
|
19
|
+
end
|
58
20
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
21
|
+
# Merges given array with current one.
|
22
|
+
#
|
23
|
+
# Will perform insertion of new items by three rules:
|
24
|
+
# 1. If the item's predecessor is present, insert item after it.
|
25
|
+
# 2. If the item's follower is present, insert item before it.
|
26
|
+
# 3. Insert item at end of list.
|
27
|
+
#
|
28
|
+
# Examples:
|
29
|
+
#
|
30
|
+
# [].merge([1, 2]) # => [1, 2]
|
31
|
+
# ['a'].merge([1, 2]) # => ['a', 1, 2]
|
32
|
+
# [1, 'a'].merge([1, 2]) # => [1, 2, 'a']
|
33
|
+
# [1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
|
34
|
+
#
|
35
|
+
def merge(array)
|
36
|
+
for value in array
|
37
|
+
next if include?(value)
|
38
|
+
if found = merging_predecessor(value, array)
|
39
|
+
position = index(found) + 1
|
40
|
+
elsif found = merging_follower(value, array)
|
41
|
+
position = index(found)
|
64
42
|
end
|
43
|
+
insert(position || length, value)
|
65
44
|
end
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# Returns predecessor of given needle from haystack.
|
51
|
+
# Helper method for #merge
|
52
|
+
def merging_predecessor(needle, haystack)
|
53
|
+
list = haystack[0..haystack.index(needle)].reverse
|
54
|
+
(list & self).first
|
55
|
+
end
|
56
|
+
|
57
|
+
# Returns follower of given needle from haystack.
|
58
|
+
# Helper method for #merge
|
59
|
+
def merging_follower(needle, haystack)
|
60
|
+
list = haystack[haystack.index(needle)+1..-1]
|
61
|
+
(list & self).first
|
66
62
|
end
|
67
63
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module FileUtils
|
2
|
+
|
3
|
+
# Removes the current directory recursively including
|
4
|
+
# all empty parent directories up to given depth.
|
5
|
+
# The current directory will be removed with all its contents,
|
6
|
+
# but parent directories will only be removed if empty.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# FileUtils.remove_dir_r("/my/nice/private/dir", 2)
|
11
|
+
# # will remove dir and, if empty, private as well
|
12
|
+
#
|
13
|
+
def self.remove_dir_r(dir, max_depth = 3)
|
14
|
+
FileUtils.remove_dir(dir)
|
15
|
+
parts = dir.split("/")
|
16
|
+
while max_depth > 1
|
17
|
+
parts.pop
|
18
|
+
max_depth -= 1
|
19
|
+
folder = File.join(parts)
|
20
|
+
break if (Dir.entries(folder) - ['.', '..']).any?
|
21
|
+
FileUtils.remove_dir(folder)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,81 +1,98 @@
|
|
1
1
|
require "uri"
|
2
2
|
|
3
|
-
|
4
|
-
module CoreExtensions
|
5
|
-
module Hash
|
6
|
-
module ClassMethods
|
7
|
-
|
8
|
-
# Returns new hash with given arguments.
|
9
|
-
# If an array is provided, it will be flattened once. Multi-level arrays are not supported.
|
10
|
-
# This method is basically a helper to support different return values of Hash#select:
|
11
|
-
# Ruby 1.8.7 returns an array, Ruby 1.9.2 returns a hash.
|
12
|
-
#
|
13
|
-
def build(args = nil)
|
14
|
-
if args.is_a?(::Array)
|
15
|
-
args = args.flatten_once
|
16
|
-
::Hash[*args]
|
17
|
-
elsif args.is_a?(::Hash)
|
18
|
-
args
|
19
|
-
else
|
20
|
-
::Hash.new
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
module InstanceMethods
|
3
|
+
class Hash
|
26
4
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
"#{URI.escape(arg[0].to_s)}=#{value}"
|
43
|
-
end
|
44
|
-
list.join("&")
|
45
|
-
end
|
46
|
-
|
47
|
-
# Returns a copy of self including only the given keys.
|
48
|
-
#
|
49
|
-
# Example:
|
50
|
-
#
|
51
|
-
# { :name => "Rodrigo", :age => 21 }.only(:name) # => { :name => "Rodrigo" }
|
52
|
-
#
|
53
|
-
# Inspired by:
|
54
|
-
# http://www.koders.com/ruby/fid80243BF76758F830B298E0E681B082B3408AB185.aspx?s=%22Rodrigo+Kochenburger%22#L9
|
55
|
-
# and
|
56
|
-
# http://snippets.dzone.com/posts/show/302
|
57
|
-
#
|
58
|
-
def only(*keys)
|
59
|
-
keys.flatten!
|
60
|
-
args = self.select { |k,v| keys.include?(k) }
|
61
|
-
::Hash.build(args)
|
62
|
-
end
|
5
|
+
# Returns new hash with given arguments.
|
6
|
+
# If an array is provided, it will be flattened once. Multi-level arrays are not supported.
|
7
|
+
# This method is basically a helper to support different return values of Hash#select:
|
8
|
+
# Ruby 1.8.7 returns an array, Ruby 1.9.2 returns a hash.
|
9
|
+
#
|
10
|
+
def self.build(args = nil)
|
11
|
+
if args.is_a?(::Array)
|
12
|
+
args = args.flatten_once
|
13
|
+
self[*args]
|
14
|
+
elsif args.is_a?(self)
|
15
|
+
args
|
16
|
+
else
|
17
|
+
self.new
|
18
|
+
end
|
19
|
+
end
|
63
20
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
21
|
+
# Returns URL-encoded string of uri params.
|
22
|
+
#
|
23
|
+
# Examples:
|
24
|
+
#
|
25
|
+
# {:some => :value, :another => "speciál"}.to_uri # => "some=value&another=speci%C3%A1l"
|
26
|
+
# {:some => {:nested => :thing}}.to_uri # => "some=[nested=thing]"
|
27
|
+
#
|
28
|
+
def to_uri
|
29
|
+
hash = dup
|
30
|
+
list = hash.to_a.map do |arg|
|
31
|
+
value = arg[1]
|
32
|
+
if value.is_a?(Hash)
|
33
|
+
value = "[#{value.to_uri}]"
|
34
|
+
else
|
35
|
+
value = URI.escape(value.to_s)
|
78
36
|
end
|
37
|
+
"#{URI.escape(arg[0].to_s)}=#{value}"
|
38
|
+
end
|
39
|
+
list.join("&")
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a copy of self including only the given keys.
|
43
|
+
#
|
44
|
+
# Example:
|
45
|
+
#
|
46
|
+
# {:name => "Rodrigo", :age => 21}.only(:name) # => {:name => "Rodrigo"}
|
47
|
+
#
|
48
|
+
# Inspired by:
|
49
|
+
# http://www.koders.com/ruby/fid80243BF76758F830B298E0E681B082B3408AB185.aspx?s=%22Rodrigo+Kochenburger%22#L9
|
50
|
+
# and
|
51
|
+
# http://snippets.dzone.com/posts/show/302
|
52
|
+
#
|
53
|
+
def only(*keys)
|
54
|
+
keys.flatten!
|
55
|
+
args = self.select { |k,v| keys.include?(k) }
|
56
|
+
Hash.build(args)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns a copy of self including all but the given keys.
|
60
|
+
#
|
61
|
+
# Example:
|
62
|
+
#
|
63
|
+
# {:name => "Rodrigo", :age = 21}.except(:name) # => {:age => 21}
|
64
|
+
#
|
65
|
+
# Inspired by:
|
66
|
+
# http://www.koders.com/ruby/fid80243BF76758F830B298E0E681B082B3408AB185.aspx?s=%22Rodrigo+Kochenburger%22#L9
|
67
|
+
#
|
68
|
+
def except(*keys)
|
69
|
+
keys.flatten!
|
70
|
+
args = self.select { |k,v| !keys.include?(k) }
|
71
|
+
Hash.build(args)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns a nested array. Just like #to_a, but nested.
|
75
|
+
#
|
76
|
+
def to_a_rec
|
77
|
+
array = []
|
78
|
+
for key, value in self
|
79
|
+
value = value.to_a_rec if value.is_a?(Hash)
|
80
|
+
array << [key, value]
|
81
|
+
end
|
82
|
+
array
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns true if hash has all of given keys.
|
86
|
+
# It's like Hash#key?, but it accepts several keys.
|
87
|
+
#
|
88
|
+
# Example:
|
89
|
+
#
|
90
|
+
# {:some => "say", :any => "thing"}.keys?(:some, :any) # => true
|
91
|
+
#
|
92
|
+
def keys?(*args)
|
93
|
+
for arg in args
|
94
|
+
return false unless self[arg]
|
79
95
|
end
|
96
|
+
return true
|
80
97
|
end
|
81
98
|
end
|
@@ -1,18 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
send(method)
|
13
|
-
rescue
|
14
|
-
end
|
15
|
-
end
|
1
|
+
class Object
|
2
|
+
|
3
|
+
# Tries to use a method on object.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
# something.try!(:else) # => nil
|
7
|
+
#
|
8
|
+
def try!(method)
|
9
|
+
begin
|
10
|
+
send(method)
|
11
|
+
rescue
|
16
12
|
end
|
17
13
|
end
|
18
14
|
end
|
@@ -1,66 +1,92 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
-
|
3
|
-
module CoreExtensions
|
4
|
-
module String
|
5
|
-
|
6
|
-
# Map of latin chars and their representations as unicode chars.
|
7
|
-
LATIN_MAP = {
|
8
|
-
"A" => %w[À Á Â Ã Å Ą Ā],
|
9
|
-
"a" => %w["à á â ã å ą ả ã ạ ă ắ ằ ẳ ẵ ặ â ấ ầ ẩ ẫ ậ ā],
|
10
|
-
"AE" => %w[Ä Æ Ǽ],
|
11
|
-
"ae" => %w[ä æ ǽ],
|
12
|
-
"C" => %w[Ç Č Ć Ĉ],
|
13
|
-
"c" => %w[ç č ć ĉ],
|
14
|
-
"D" => %w[Ð],
|
15
|
-
"d" => %w[đ],
|
16
|
-
"E" => %w[È É Ê Ẽ Ę Ė Ē Ë],
|
17
|
-
"e" => %w[è é ę ë ė ẻ ẽ ẹ ê ế ề ể ễ ệ ē],
|
18
|
-
"G" => %w[Ģ],
|
19
|
-
"g" => %w[ģ],
|
20
|
-
"I" => %w[Ì Í Î Ï Ĩ Į Ī],
|
21
|
-
"i" => %w[ì í î ï ĩ į ỉ ị ī],
|
22
|
-
"K" => %w[Ķ],
|
23
|
-
"k" => %w[ķ],
|
24
|
-
"L" => %w[Ļ],
|
25
|
-
"l" => %w[ļ],
|
26
|
-
"N" => %w[Ñ Ń Ņ],
|
27
|
-
"n" => %w[ñ ń ņ],
|
28
|
-
"O" => %w[Ò Ó Ô Õ Ø],
|
29
|
-
"o" => %w[ò ó õ ỏ õ ọ ô ố ồ ổ ỗ ộ ơ ớ ờ ở ỡ ợ ø],
|
30
|
-
"OE" => %w[Ö Œ],
|
31
|
-
"oe" => %w[ö œ],
|
32
|
-
"R" => %w[Ŗ],
|
33
|
-
"r" => %w[ŗ],
|
34
|
-
"S" => %w[Š],
|
35
|
-
"s" => %w[š],
|
36
|
-
"ss" => %w[ß],
|
37
|
-
"U" => %w[Ù Ú Ũ Ű Ů Ũ Ų Ū Û],
|
38
|
-
"u" => %w[ų ū û ú ù ű ů ủ ũ ụ ư ứ ừ ử ữ ự],
|
39
|
-
"UE" => %w[Ü],
|
40
|
-
"ue" => %w[ü],
|
41
|
-
"x" => %w[×],
|
42
|
-
"Y" => %w[Ý Ÿ Ŷ],
|
43
|
-
"y" => %w[ý ÿ ŷ ỳ ỷ ỹ ỵ],
|
44
|
-
"Z" => %w[Ž],
|
45
|
-
"z" => %w[ž]
|
46
|
-
}.freeze
|
2
|
+
class String
|
47
3
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
4
|
+
# Map of latin chars and their representations as unicode chars.
|
5
|
+
LATIN_MAP = {
|
6
|
+
"A" => %w[À Á Â Ã Å Ą Ā],
|
7
|
+
"a" => %w["à á â ã å ą ả ã ạ ă ắ ằ ẳ ẵ ặ â ấ ầ ẩ ẫ ậ ā],
|
8
|
+
"AE" => %w[Ä Æ Ǽ],
|
9
|
+
"ae" => %w[ä æ ǽ],
|
10
|
+
"C" => %w[Ç Č Ć Ĉ],
|
11
|
+
"c" => %w[ç č ć ĉ],
|
12
|
+
"D" => %w[Ð],
|
13
|
+
"d" => %w[đ],
|
14
|
+
"E" => %w[È É Ê Ẽ Ę Ė Ē Ë],
|
15
|
+
"e" => %w[è é ę ë ė ẻ ẽ ẹ ê ế ề ể ễ ệ ē],
|
16
|
+
"G" => %w[Ģ],
|
17
|
+
"g" => %w[ģ],
|
18
|
+
"I" => %w[Ì Í Î Ï Ĩ Į Ī],
|
19
|
+
"i" => %w[ì í î ï ĩ į ỉ ị ī],
|
20
|
+
"K" => %w[Ķ],
|
21
|
+
"k" => %w[ķ],
|
22
|
+
"L" => %w[Ļ],
|
23
|
+
"l" => %w[ļ],
|
24
|
+
"N" => %w[Ñ Ń Ņ],
|
25
|
+
"n" => %w[ñ ń ņ],
|
26
|
+
"O" => %w[Ò Ó Ô Õ Ø],
|
27
|
+
"o" => %w[ò ó õ ỏ õ ọ ô ố ồ ổ ỗ ộ ơ ớ ờ ở ỡ ợ ø],
|
28
|
+
"OE" => %w[Ö Œ],
|
29
|
+
"oe" => %w[ö œ],
|
30
|
+
"R" => %w[Ŗ],
|
31
|
+
"r" => %w[ŗ],
|
32
|
+
"S" => %w[Š],
|
33
|
+
"s" => %w[š],
|
34
|
+
"ss" => %w[ß],
|
35
|
+
"U" => %w[Ù Ú Ũ Ű Ů Ũ Ų Ū Û],
|
36
|
+
"u" => %w[ų ū û ú ù ű ů ủ ũ ụ ư ứ ừ ử ữ ự],
|
37
|
+
"UE" => %w[Ü],
|
38
|
+
"ue" => %w[ü],
|
39
|
+
"x" => %w[×],
|
40
|
+
"Y" => %w[Ý Ÿ Ŷ],
|
41
|
+
"y" => %w[ý ÿ ŷ ỳ ỷ ỹ ỵ],
|
42
|
+
"Z" => %w[Ž],
|
43
|
+
"z" => %w[ž]
|
44
|
+
}.freeze
|
45
|
+
|
46
|
+
# Replaces non-latin chars, leaves some special ones.
|
47
|
+
def latinize
|
48
|
+
c = clone
|
49
|
+
for char, map in LATIN_MAP
|
50
|
+
c.gsub!(/(#{map.join('|')})/, char)
|
51
|
+
end
|
52
|
+
c.gsub(/[^a-z0-9\.\,\|\?\!\:;"'=\+\-_]+/i, " ").
|
53
|
+
gsub(/ {2,}/, " ")
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns a string that may be used as permalink
|
57
|
+
def permalink
|
58
|
+
latinize.
|
59
|
+
downcase.
|
60
|
+
gsub(/[^a-z0-9]+/, "-").
|
61
|
+
gsub(/^-/, "").gsub(/-$/, "")
|
62
|
+
end
|
63
|
+
|
64
|
+
# Extends Kernel::sprintf to accept "named arguments" given as hash.
|
65
|
+
# This method was taken from Ruby-GetText. Thank you!
|
66
|
+
#
|
67
|
+
# Examples:
|
68
|
+
#
|
69
|
+
# # Normal sprintf behaviour:
|
70
|
+
# "%s, %s" % ["Masao", "Mutoh"]
|
71
|
+
#
|
72
|
+
# # Extended version with named arguments:
|
73
|
+
# "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
|
74
|
+
#
|
75
|
+
alias :_sprintf :% # :nodoc:
|
76
|
+
def %(args)
|
77
|
+
if args.kind_of?(Hash)
|
78
|
+
ret = dup
|
79
|
+
args.each do |key, value|
|
80
|
+
ret.gsub!(/\%\{#{key}\}/, value.to_s)
|
56
81
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
82
|
+
ret
|
83
|
+
else
|
84
|
+
ret = gsub(/%\{/, '%%{')
|
85
|
+
begin
|
86
|
+
ret._sprintf(args)
|
87
|
+
rescue ArgumentError
|
88
|
+
$stderr.puts " The string:#{ret}"
|
89
|
+
$stderr.puts " args:#{args.inspect}"
|
64
90
|
end
|
65
91
|
end
|
66
92
|
end
|
@@ -2,9 +2,4 @@ require "core_extensions/object"
|
|
2
2
|
require "core_extensions/hash"
|
3
3
|
require "core_extensions/array"
|
4
4
|
require "core_extensions/string"
|
5
|
-
|
6
|
-
Object.send :include, Vidibus::CoreExtensions::Object
|
7
|
-
Hash.send :extend, Vidibus::CoreExtensions::Hash::ClassMethods
|
8
|
-
Hash.send :include, Vidibus::CoreExtensions::Hash::InstanceMethods
|
9
|
-
Array.send :include, Vidibus::CoreExtensions::Array
|
10
|
-
String.send :include, Vidibus::CoreExtensions::String
|
5
|
+
require "core_extensions/file_utils"
|
data/spec/spec_helper.rb
CHANGED
@@ -6,17 +6,17 @@ describe "Vidibus::CoreExtensions::Array" do
|
|
6
6
|
array = ['go', 'for', 'it']
|
7
7
|
array.flatten_once.should eql(['go', 'for', 'it'])
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
it "should return a flattened array" do
|
11
11
|
array = ['go', ['for', 'it']]
|
12
12
|
array.flatten_once.should eql(['go', 'for', 'it'])
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
it "should flatten first level only" do
|
16
16
|
array = ['go', ['for', ['it']]]
|
17
17
|
array.flatten_once.should eql(['go', 'for', ['it']])
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it "should accept array with mixed values" do
|
21
21
|
array = ["go", [1,2], { :it => "dude" }]
|
22
22
|
array.flatten_once.should eql(["go", 1, 2, { :it => "dude" }])
|
@@ -27,31 +27,31 @@ describe "Vidibus::CoreExtensions::Array" do
|
|
27
27
|
it "should merge [] with [1,2]" do
|
28
28
|
[].merge([1,2]).should eql([1,2])
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
it "should merge [a] with [1,2]" do
|
32
32
|
['a'].merge([1,2]).should eql(['a',1,2])
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
it "should merge [1,'a'] with [1,2]" do
|
36
36
|
[1,'a'].merge([1,2]).should eql([1,2,'a'])
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
it "should merge [1,'a'] with [3,1,2]" do
|
40
40
|
[1,'a'].merge([3,1,2]).should eql([3,1,2,'a'])
|
41
41
|
end
|
42
|
-
|
42
|
+
|
43
43
|
it "should merge ['b',1,'a'] with [3,1,2]" do
|
44
44
|
['b',1,'a'].merge([3,1,2]).should eql(['b',3,1,2,'a'])
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
it "should merge [2,'b',1,'a'] with [3,1,2]" do
|
48
48
|
[2,'b',1,'a'].merge([3,1,2]).should eql([2,'b',3,1,'a'])
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
it "should merge [2,'b',1,'a'] with [3,1,2,4]" do
|
52
52
|
[2,'b',1,'a'].merge([3,1,2,4]).should eql([2,4,'b',3,1,'a'])
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
it "should merge [2,'b',1,'a'] with [5,3,6,7,1,2,4]" do
|
56
56
|
[2,'b',1,'a'].merge([5,3,6,7,1,2,4]).should eql([2,4,'b',5,3,6,7,1,'a'])
|
57
57
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Vidibus::CoreExtensions::FileUtils" do
|
4
|
+
describe ".remove_dir_r" do
|
5
|
+
let(:tmpdir) { File.join(File.dirname(__FILE__), "..", "..", "tmp") }
|
6
|
+
before { FileUtils.mkdir_p("#{tmpdir}/some/test/dir")}
|
7
|
+
|
8
|
+
it "should remove the current directory and all its parents up to three levels deep" do
|
9
|
+
FileUtils.remove_dir_r("#{tmpdir}/some/test/dir")
|
10
|
+
File.exist?("#{tmpdir}/some").should be_false
|
11
|
+
File.exist?(tmpdir).should be_true
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not remove parent directory unless it is emtpy" do
|
15
|
+
FileUtils.touch("#{tmpdir}/some/textfile.txt")
|
16
|
+
FileUtils.remove_dir_r("#{tmpdir}/some/test/dir")
|
17
|
+
File.exist?("#{tmpdir}/some").should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should remove all child directories recursively" do
|
21
|
+
FileUtils.mkdir_p("#{tmpdir}/some/test/dir/with/some/children")
|
22
|
+
FileUtils.remove_dir_r("#{tmpdir}/some/test/dir")
|
23
|
+
File.exist?("#{tmpdir}/some").should be_false
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should remove parent directories up to provided depth only" do
|
27
|
+
FileUtils.remove_dir_r("#{tmpdir}/some/test/dir", 2)
|
28
|
+
File.exist?("#{tmpdir}/some").should be_true
|
29
|
+
end
|
30
|
+
|
31
|
+
after { FileUtils.remove_dir(tmpdir)}
|
32
|
+
end
|
33
|
+
end
|
@@ -3,75 +3,103 @@ require "spec_helper"
|
|
3
3
|
describe "Vidibus::CoreExtensions::Hash" do
|
4
4
|
describe "#to_uri" do
|
5
5
|
it "should join params with '&'" do
|
6
|
-
hash = {
|
6
|
+
hash = {:some => "value", :another => "thing"}
|
7
7
|
parts = hash.to_uri.split("&")
|
8
8
|
parts.sort.should eql(['another=thing', 'some=value'])
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
it "should return items as urlencoded string" do
|
12
|
-
hash = {
|
12
|
+
hash = {:another => "speciál"}
|
13
13
|
hash.to_uri.should eql("another=speci%C3%A1l")
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "should support multi-level hashes" do
|
17
|
-
hash = {
|
17
|
+
hash = {:some => {:nested => :thing}}
|
18
18
|
hash.to_uri.should eql("some=[nested=thing]")
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
describe "#only" do
|
23
23
|
it "should return a copy of self but including only the given keys" do
|
24
|
-
hash = {
|
25
|
-
hash.only(:name).should eql({
|
24
|
+
hash = {:name => "rodrigo", :age => 21}
|
25
|
+
hash.only(:name).should eql({:name => "rodrigo"})
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "should work with array as parameter" do
|
29
|
-
hash = {
|
30
|
-
hash.only([:name, :something]).should eql({
|
29
|
+
hash = {:name => "rodrigo", :age => 21}
|
30
|
+
hash.only([:name, :something]).should eql({:name => "rodrigo"})
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
it "should work for nested hash" do
|
34
|
-
hash = {
|
35
|
-
hash.only(:name, :girlfriends).should eql({
|
34
|
+
hash = {:name => "rodrigo", :girlfriends => ["Anna", "Maria"]}
|
35
|
+
hash.only(:name, :girlfriends).should eql({:name => "rodrigo", :girlfriends => ["Anna", "Maria"]})
|
36
36
|
end
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
describe "#except" do
|
40
40
|
it "should return a copy of self but including only the given keys" do
|
41
|
-
hash = {
|
42
|
-
hash.except(:name).should eql({
|
41
|
+
hash = {:name => "rodrigo", :age => 21}
|
42
|
+
hash.except(:name).should eql({:age => 21})
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
it "should work with array as parameter" do
|
46
|
-
hash = {
|
47
|
-
hash.except([:name, :something]).should eql({
|
46
|
+
hash = {:name => "rodrigo", :age => 21}
|
47
|
+
hash.except([:name, :something]).should eql({:age => 21})
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it "should work for nested hash" do
|
51
|
-
hash = {
|
52
|
-
hash.except(:name).should eql({
|
51
|
+
hash = {:name => "rodrigo", :girlfriends => ["Anna", "Maria"]}
|
52
|
+
hash.except(:name).should eql({:girlfriends => ["Anna", "Maria"]})
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#to_a_rec" do
|
57
|
+
it "should return an array" do
|
58
|
+
hash = {:some => "thing"}
|
59
|
+
hash.to_a_rec.should eql([[:some, "thing"]])
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should return an array of from nested attributes" do
|
63
|
+
hash = {:some => {:nested => {:is => ["really", "groovy"]}}}
|
64
|
+
hash.to_a_rec.should eql([[:some, [[:nested, [[:is, ["really", "groovy"]]]]]]])
|
53
65
|
end
|
54
66
|
end
|
55
|
-
|
67
|
+
|
68
|
+
describe "#keys?" do
|
69
|
+
let(:hash) { {:some => "say", :any => "thing"} }
|
70
|
+
|
71
|
+
it "should return true if all keys are given in hash" do
|
72
|
+
hash.keys?(:some, :any).should be_true
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return true if all keys are given in larger hash" do
|
76
|
+
hash.keys?(:any).should be_true
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return false if any of the given key misses in hash" do
|
80
|
+
hash.keys?(:any, :thing).should be_false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
56
84
|
describe ".build" do
|
57
85
|
it "should return a hash" do
|
58
86
|
Hash.build.should eql(Hash.new)
|
59
87
|
end
|
60
|
-
|
88
|
+
|
61
89
|
it "should accept a hash" do
|
62
|
-
Hash.build({
|
90
|
+
Hash.build({:do => :it}).should eql({:do => :it})
|
63
91
|
end
|
64
|
-
|
92
|
+
|
65
93
|
it "should accept an array" do
|
66
|
-
Hash.build([:do, :it]).should eql({
|
94
|
+
Hash.build([:do, :it]).should eql({:do => :it})
|
67
95
|
end
|
68
|
-
|
96
|
+
|
69
97
|
it "should accept an array and flatten it once" do
|
70
|
-
Hash.build([:do, [:it]]).should eql({
|
98
|
+
Hash.build([:do, [:it]]).should eql({:do => :it})
|
71
99
|
end
|
72
|
-
|
100
|
+
|
73
101
|
it "should not accept a multi-level array" do
|
74
|
-
expect {
|
102
|
+
expect {Hash.build([:do, [:it, [:now]]])}.to raise_error(ArgumentError, "odd number of arguments for Hash")
|
75
103
|
end
|
76
104
|
end
|
77
105
|
end
|
@@ -7,60 +7,72 @@ describe "Vidibus::CoreExtensions::String" do
|
|
7
7
|
String::LATIN_MAP.should be_a(Hash)
|
8
8
|
end
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
describe "#latinize" do
|
12
12
|
it "should convert diacritics" do
|
13
|
-
"ÀÁÂÃÄÅ Ç Ð ÈÉÊË ÌÍÎÏ Ñ ÒÓÔÕÖØ ÙÚÛÜ Ý àáâãäå ç èéêë ìíîï ñ òóôõöø ùúûü ý".latinize.should
|
13
|
+
"ÀÁÂÃÄÅ Ç Ð ÈÉÊË ÌÍÎÏ Ñ ÒÓÔÕÖØ ÙÚÛÜ Ý àáâãäå ç èéêë ìíîï ñ òóôõöø ùúûü ý".latinize.should
|
14
14
|
eql("AAAAAEA C D EEEE IIII N OOOOOEO UUUUE Y aaaaaea c eeee iiii n oooooeo uuuue y")
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it "should convert ligatures" do
|
18
18
|
"Æ".latinize.should eql("AE")
|
19
19
|
"ÆǼ æǽ Œ œ".latinize.should eql("AEAE aeae OE oe")
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should keep some regular chars" do
|
23
23
|
".,|?!:;\"'=+-_".latinize.should eql(".,|?!:;\"'=+-_")
|
24
24
|
end
|
25
|
-
|
25
|
+
|
26
26
|
it "should replace exotic chars by whitespace" do
|
27
27
|
"~÷≥≤˛`ˀð".latinize.should eql(" ")
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
it "should normalize white space" do
|
31
31
|
"Hola señor, ¿cómo está?".latinize.should eql("Hola senor, como esta?")
|
32
32
|
end
|
33
33
|
end
|
34
|
-
|
34
|
+
|
35
35
|
describe "#permalink" do
|
36
36
|
it "should call #latinize" do
|
37
37
|
string = "hey"
|
38
38
|
mock(string).latinize { string }
|
39
39
|
string.permalink.should eql(string)
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "should return lower chars only" do
|
43
43
|
"HeLlo".permalink.should eql("hello")
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
it "should turn whitespace into dashes" do
|
47
47
|
"hey joe".permalink.should eql("hey-joe")
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
it "should turn special chars into dashes" do
|
51
51
|
"hi~there".permalink.should eql("hi-there")
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it "should not begin with dashes" do
|
55
55
|
">duh".permalink.should eql("duh")
|
56
56
|
end
|
57
|
-
|
57
|
+
|
58
58
|
it "should not end with dashes" do
|
59
59
|
"hi!".permalink.should eql("hi")
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
it "should convert multiple adjacent special chars into a single dash" do
|
63
63
|
"Hola señor, ¿cómo está?".permalink.should eql("hola-senor-como-esta")
|
64
64
|
end
|
65
65
|
end
|
66
|
+
|
67
|
+
describe "#%" do
|
68
|
+
it "should allow reqular printf behaviour" do
|
69
|
+
string = "%s, %s" % ["Masao", "Mutoh"]
|
70
|
+
string.should eql("Masao, Mutoh")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should accept named arguments" do
|
74
|
+
string = "%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
|
75
|
+
string.should eql("Masao, Mutoh")
|
76
|
+
end
|
77
|
+
end
|
66
78
|
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{vidibus-core_extensions}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.6"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andre Pankratz"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-23}
|
13
13
|
s.description = %q{Provides some extensions to the ruby core.}
|
14
14
|
s.email = %q{andre@vidibus.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,11 +30,13 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/vidibus-core_extensions.rb",
|
31
31
|
"lib/vidibus/core_extensions.rb",
|
32
32
|
"lib/vidibus/core_extensions/array.rb",
|
33
|
+
"lib/vidibus/core_extensions/file_utils.rb",
|
33
34
|
"lib/vidibus/core_extensions/hash.rb",
|
34
35
|
"lib/vidibus/core_extensions/object.rb",
|
35
36
|
"lib/vidibus/core_extensions/string.rb",
|
36
37
|
"spec/spec_helper.rb",
|
37
38
|
"spec/vidibus/core_extensions/array_spec.rb",
|
39
|
+
"spec/vidibus/core_extensions/file_utils_spec.rb",
|
38
40
|
"spec/vidibus/core_extensions/hash_spec.rb",
|
39
41
|
"spec/vidibus/core_extensions/object_spec.rb",
|
40
42
|
"spec/vidibus/core_extensions/string_spec.rb",
|
@@ -48,6 +50,7 @@ Gem::Specification.new do |s|
|
|
48
50
|
s.test_files = [
|
49
51
|
"spec/spec_helper.rb",
|
50
52
|
"spec/vidibus/core_extensions/array_spec.rb",
|
53
|
+
"spec/vidibus/core_extensions/file_utils_spec.rb",
|
51
54
|
"spec/vidibus/core_extensions/hash_spec.rb",
|
52
55
|
"spec/vidibus/core_extensions/object_spec.rb",
|
53
56
|
"spec/vidibus/core_extensions/string_spec.rb"
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vidibus-core_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 6
|
10
|
+
version: 0.3.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andre Pankratz
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-23 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -42,11 +42,13 @@ files:
|
|
42
42
|
- lib/vidibus-core_extensions.rb
|
43
43
|
- lib/vidibus/core_extensions.rb
|
44
44
|
- lib/vidibus/core_extensions/array.rb
|
45
|
+
- lib/vidibus/core_extensions/file_utils.rb
|
45
46
|
- lib/vidibus/core_extensions/hash.rb
|
46
47
|
- lib/vidibus/core_extensions/object.rb
|
47
48
|
- lib/vidibus/core_extensions/string.rb
|
48
49
|
- spec/spec_helper.rb
|
49
50
|
- spec/vidibus/core_extensions/array_spec.rb
|
51
|
+
- spec/vidibus/core_extensions/file_utils_spec.rb
|
50
52
|
- spec/vidibus/core_extensions/hash_spec.rb
|
51
53
|
- spec/vidibus/core_extensions/object_spec.rb
|
52
54
|
- spec/vidibus/core_extensions/string_spec.rb
|
@@ -88,6 +90,7 @@ summary: Extends the ruby core.
|
|
88
90
|
test_files:
|
89
91
|
- spec/spec_helper.rb
|
90
92
|
- spec/vidibus/core_extensions/array_spec.rb
|
93
|
+
- spec/vidibus/core_extensions/file_utils_spec.rb
|
91
94
|
- spec/vidibus/core_extensions/hash_spec.rb
|
92
95
|
- spec/vidibus/core_extensions/object_spec.rb
|
93
96
|
- spec/vidibus/core_extensions/string_spec.rb
|