mpatch 1.3.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/README.md +32 -0
- data/VERSION +1 -1
- data/lib/mpatch/array.rb +119 -117
- data/lib/mpatch/class.rb +68 -66
- data/lib/mpatch/file.rb +35 -37
- data/lib/mpatch/hash.rb +93 -91
- data/lib/mpatch/integer.rb +10 -8
- data/lib/mpatch/module.rb +13 -11
- data/lib/mpatch/object.rb +211 -196
- data/lib/mpatch/proc.rb +11 -9
- data/lib/mpatch/process.rb +11 -22
- data/lib/mpatch/random.rb +41 -32
- data/lib/mpatch/string.rb +65 -63
- data/lib/mpatch/yml.rb +11 -7
- data/lib/mpatch.rb +33 -0
- data/mpatch.gemspec +0 -2
- metadata +6 -24
- data/lib/mpatch/boolean.rb +0 -15
- data/lib/mpatch/exception.rb +0 -0
- data/lib/mpatch/kernel.rb +0 -10
- data/lib/mpatch/str2duck.rb +0 -1
data/lib/mpatch/hash.rb
CHANGED
@@ -1,109 +1,111 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
self
|
1
|
+
module MPatch
|
2
|
+
module Hash
|
3
|
+
|
4
|
+
# remove elements by keys,
|
5
|
+
# array of keys,
|
6
|
+
# hashTags,
|
7
|
+
# strings
|
8
|
+
def trim(*args)
|
9
|
+
|
10
|
+
args.each do |one_element|
|
11
|
+
case true
|
12
|
+
|
13
|
+
when one_element.class <= ::Hash
|
14
|
+
begin
|
15
|
+
one_element.each do |key,value|
|
16
|
+
if self[key] == value
|
17
|
+
self.delete(key)
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
19
|
-
end
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
22
|
+
when one_element.class <= ::Array
|
23
|
+
begin
|
24
|
+
one_element.each do |one_key|
|
25
|
+
self.delete(one_key)
|
26
|
+
end
|
25
27
|
end
|
26
|
-
end
|
27
28
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
when one_element.class <= ::Symbol,
|
30
|
+
one_element.class <= ::String
|
31
|
+
begin
|
32
|
+
self.delete(one_element)
|
33
|
+
end
|
33
34
|
|
35
|
+
end
|
34
36
|
end
|
35
|
-
|
36
|
-
return self
|
37
|
-
|
38
|
-
end
|
37
|
+
return self
|
39
38
|
|
40
|
-
|
41
|
-
def remove!(*keys)
|
42
|
-
keys.each{|key| self.delete(key) }
|
43
|
-
self
|
44
|
-
end
|
45
|
-
|
46
|
-
#non-destructive version
|
47
|
-
def remove(*keys)
|
48
|
-
self.dup.remove!(*keys)
|
49
|
-
end
|
39
|
+
end
|
50
40
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
#
|
56
|
-
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
|
57
|
-
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
|
58
|
-
def deep_merge(other_hash)
|
59
|
-
dup.deep_merge!(other_hash)
|
60
|
-
end unless method_defined? :deep_merge
|
61
|
-
|
62
|
-
alias :+ :deep_merge
|
63
|
-
|
64
|
-
# Same as +deep_merge+, but modifies +self+.
|
65
|
-
def deep_merge!(other_hash)
|
66
|
-
other_hash.each_pair do |k,v|
|
67
|
-
tv = self[k]
|
68
|
-
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
41
|
+
#pass single or array of keys, which will be removed, returning the remaining hash
|
42
|
+
def remove!(*keys)
|
43
|
+
keys.each{|key| self.delete(key) }
|
44
|
+
self
|
69
45
|
end
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
def deep_include?(sub_hash)
|
75
|
-
sub_hash.keys.all? do |key|
|
76
|
-
self.has_key?(key) && if sub_hash[key].is_a?(Hash)
|
77
|
-
self[key].is_a?(Hash) && self[key].deep_include?(sub_hash[key])
|
78
|
-
else
|
79
|
-
self[key] == sub_hash[key]
|
80
|
-
end
|
46
|
+
|
47
|
+
#non-destructive version
|
48
|
+
def remove(*keys)
|
49
|
+
self.dup.remove!(*keys)
|
81
50
|
end
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
51
|
+
|
52
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
53
|
+
#
|
54
|
+
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
|
55
|
+
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
|
56
|
+
#
|
57
|
+
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
|
58
|
+
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
|
59
|
+
def deep_merge(other_hash)
|
60
|
+
dup.deep_merge!(other_hash)
|
61
|
+
end unless method_defined? :deep_merge
|
62
|
+
|
63
|
+
alias :+ :deep_merge
|
64
|
+
|
65
|
+
# Same as +deep_merge+, but modifies +self+.
|
66
|
+
def deep_merge!(other_hash)
|
67
|
+
other_hash.each_pair do |k,v|
|
68
|
+
tv = self[k]
|
69
|
+
self[k] = tv.is_a?(::Hash) && v.is_a?(::Hash) ? tv.deep_merge(v) : v
|
70
|
+
end
|
71
|
+
self
|
72
|
+
end unless method_defined? :deep_merge!
|
73
|
+
|
74
|
+
# return bool that does the sub hash all element include the hash who call this or not
|
75
|
+
def deep_include?(sub_hash)
|
76
|
+
sub_hash.keys.all? do |key|
|
77
|
+
self.has_key?(key) && if sub_hash[key].is_a?(::Hash)
|
78
|
+
self[key].is_a?(::Hash) && self[key].deep_include?(sub_hash[key])
|
79
|
+
else
|
80
|
+
self[key] == sub_hash[key]
|
81
|
+
end
|
100
82
|
end
|
83
|
+
end unless method_defined? :deep_include?
|
84
|
+
|
85
|
+
# map hash will work just alike map but instead of an array it will return a hash obj
|
86
|
+
#
|
87
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| [ k , 123] }
|
88
|
+
# #> {:hello=>123, :world=>123}
|
89
|
+
#
|
90
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
|
91
|
+
# #> {:hello=>123, :world=>123}
|
92
|
+
#
|
93
|
+
def map_hash &block
|
94
|
+
|
95
|
+
tmp_hash= self.class.new
|
96
|
+
map_hash_obj= self.map &block
|
97
|
+
map_hash_obj.each do |hash|
|
101
98
|
|
102
|
-
|
99
|
+
if hash.class <= ::Array
|
100
|
+
hash= self.class[*hash]
|
101
|
+
end
|
103
102
|
|
103
|
+
tmp_hash.deep_merge!(hash)
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
return tmp_hash
|
104
108
|
end
|
105
109
|
|
106
|
-
return tmp_hash
|
107
110
|
end
|
108
|
-
|
109
111
|
end
|
data/lib/mpatch/integer.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
module MPatch
|
3
|
+
module Integer
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
# because for i in integer/fixnum not working,
|
6
|
+
# here is a little patch
|
7
|
+
def each &block
|
8
|
+
self.times do |number|
|
9
|
+
block.call number
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/mpatch/module.rb
CHANGED
@@ -1,16 +1,18 @@
|
|
1
|
-
|
1
|
+
module MPatch
|
2
|
+
class Module
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# return the module objects direct sub modules
|
5
|
+
def modules
|
6
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Module}
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
# return the module objects direct sub modules
|
10
|
+
def classes
|
11
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Class}
|
12
|
+
end
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
alias :submodules :modules
|
15
|
+
alias :subclasses :classes
|
15
16
|
|
17
|
+
end
|
16
18
|
end
|