mpatch 2.3.0 → 2.4.0
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.
- checksums.yaml +4 -4
- data/README.md +92 -42
- data/VERSION +1 -1
- data/examples/test.rb +6 -3
- data/lib/mpatch/all.rb +7 -0
- data/lib/mpatch/array.rb +123 -115
- data/lib/mpatch/class.rb +75 -66
- data/lib/mpatch/hash.rb +100 -91
- data/lib/mpatch/injector.rb +62 -0
- data/lib/mpatch/integer.rb +15 -7
- data/lib/mpatch/module.rb +56 -45
- data/lib/mpatch/object.rb +127 -118
- data/lib/mpatch/proc.rb +19 -10
- data/lib/mpatch/process.rb +18 -9
- data/lib/mpatch/random.rb +34 -25
- data/lib/mpatch/string.rb +73 -64
- data/lib/mpatch/yml.rb +14 -7
- data/lib/mpatch.rb +1 -44
- metadata +4 -2
data/lib/mpatch/hash.rb
CHANGED
@@ -1,111 +1,120 @@
|
|
1
|
-
module MPatch
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
module MPatch
|
2
|
+
|
3
|
+
module Include
|
4
|
+
|
5
|
+
module Hash
|
6
|
+
|
7
|
+
# remove elements by keys,
|
8
|
+
# array of keys,
|
9
|
+
# hashTags,
|
10
|
+
# strings
|
11
|
+
def trim(*args)
|
12
|
+
|
13
|
+
args.each do |one_element|
|
14
|
+
case true
|
15
|
+
|
16
|
+
when one_element.class <= ::Hash
|
17
|
+
begin
|
18
|
+
one_element.each do |key,value|
|
19
|
+
if self[key] == value
|
20
|
+
self.delete(key)
|
21
|
+
end
|
18
22
|
end
|
19
23
|
end
|
20
|
-
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
when one_element.class <= ::Array
|
26
|
+
begin
|
27
|
+
one_element.each do |one_key|
|
28
|
+
self.delete(one_key)
|
29
|
+
end
|
26
30
|
end
|
27
|
-
end
|
28
31
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
when one_element.class <= ::Symbol,
|
33
|
+
one_element.class <= ::String
|
34
|
+
begin
|
35
|
+
self.delete(one_element)
|
36
|
+
end
|
34
37
|
|
38
|
+
end
|
35
39
|
end
|
36
|
-
|
37
|
-
return self
|
38
|
-
|
39
|
-
end
|
40
|
+
return self
|
40
41
|
|
41
|
-
|
42
|
-
def remove!(*keys)
|
43
|
-
keys.each{|key| self.delete(key) }
|
44
|
-
self
|
45
|
-
end
|
46
|
-
|
47
|
-
#non-destructive version
|
48
|
-
def remove(*keys)
|
49
|
-
self.dup.remove!(*keys)
|
50
|
-
end
|
42
|
+
end
|
51
43
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
44
|
+
#pass single or array of keys, which will be removed, returning the remaining hash
|
45
|
+
def remove!(*keys)
|
46
|
+
keys.each{|key| self.delete(key) }
|
47
|
+
self
|
70
48
|
end
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
49
|
+
|
50
|
+
#non-destructive version
|
51
|
+
def remove(*keys)
|
52
|
+
self.dup.remove!(*keys)
|
82
53
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
54
|
+
|
55
|
+
# Returns a new hash with +self+ and +other_hash+ merged recursively.
|
56
|
+
#
|
57
|
+
# h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
|
58
|
+
# h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
|
59
|
+
#
|
60
|
+
# h1.deep_merge(h2) #=> { :x => {:y => [7, 8, 9]}, :z => "xyz" }
|
61
|
+
# h2.deep_merge(h1) #=> { :x => {:y => [4, 5, 6]}, :z => [7, 8, 9] }
|
62
|
+
def deep_merge(other_hash)
|
63
|
+
dup.deep_merge!(other_hash)
|
64
|
+
end unless method_defined? :deep_merge
|
65
|
+
|
66
|
+
alias :+ :deep_merge
|
67
|
+
|
68
|
+
# Same as +deep_merge+, but modifies +self+.
|
69
|
+
def deep_merge!(other_hash)
|
70
|
+
other_hash.each_pair do |k,v|
|
71
|
+
tv = self[k]
|
72
|
+
self[k] = tv.is_a?(::Hash) && v.is_a?(::Hash) ? tv.deep_merge(v) : v
|
101
73
|
end
|
74
|
+
self
|
75
|
+
end unless method_defined? :deep_merge!
|
76
|
+
|
77
|
+
# return bool that does the sub hash all element include the hash who call this or not
|
78
|
+
def deep_include?(sub_hash)
|
79
|
+
sub_hash.keys.all? do |key|
|
80
|
+
self.has_key?(key) && if sub_hash[key].is_a?(::Hash)
|
81
|
+
self[key].is_a?(::Hash) && self[key].deep_include?(sub_hash[key])
|
82
|
+
else
|
83
|
+
self[key] == sub_hash[key]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end unless method_defined? :deep_include?
|
87
|
+
|
88
|
+
# map hash will work just alike map but instead of an array it will return a hash obj
|
89
|
+
#
|
90
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| [ k , 123] }
|
91
|
+
# #> {:hello=>123, :world=>123}
|
92
|
+
#
|
93
|
+
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
|
94
|
+
# #> {:hello=>123, :world=>123}
|
95
|
+
#
|
96
|
+
def map_hash &block
|
97
|
+
|
98
|
+
tmp_hash= self.class.new
|
99
|
+
map_hash_obj= self.map &block
|
100
|
+
map_hash_obj.each do |hash|
|
102
101
|
|
103
|
-
|
102
|
+
if hash.class <= ::Array
|
103
|
+
hash= self.class[*hash]
|
104
|
+
end
|
104
105
|
|
106
|
+
tmp_hash.deep_merge!(hash)
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
return tmp_hash
|
105
111
|
end
|
106
112
|
|
107
|
-
return tmp_hash
|
108
113
|
end
|
109
114
|
|
110
115
|
end
|
116
|
+
|
117
|
+
require File.join 'mpatch','injector'
|
118
|
+
|
119
|
+
|
111
120
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module MPatch
|
2
|
+
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def inject_patches
|
6
|
+
|
7
|
+
#begin
|
8
|
+
# @@valid_inject_cmd.nil?
|
9
|
+
#rescue ::NameError
|
10
|
+
# return nil
|
11
|
+
#end
|
12
|
+
|
13
|
+
self.submodules.each do |module_name|
|
14
|
+
|
15
|
+
method_name= module_name.to_s.split('::').last.downcase.to_s.to_sym
|
16
|
+
|
17
|
+
module_name.__send__ :extend, MPatch::Include::Module
|
18
|
+
module_name.submodules.each do |sub_module_name|
|
19
|
+
|
20
|
+
constant= ::Object
|
21
|
+
constant_name= sub_module_name.to_s.split('::').last
|
22
|
+
array_of_target_constant= []
|
23
|
+
|
24
|
+
case true
|
25
|
+
|
26
|
+
when sub_module_name.to_s.include?('And')
|
27
|
+
sub_module_name.to_s.split('::').last.split('And').each do |tag_module|
|
28
|
+
array_of_target_constant.push tag_module
|
29
|
+
end
|
30
|
+
|
31
|
+
else
|
32
|
+
array_of_target_constant.push constant_name
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
array_of_target_constant.each do |name|
|
37
|
+
|
38
|
+
begin
|
39
|
+
target_constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
40
|
+
target_constant.__send__ method_name, sub_module_name
|
41
|
+
rescue ::NoMethodError => ex
|
42
|
+
STDERR.puts ex
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
alias :inject :inject_patches
|
54
|
+
alias :patch! :inject_patches
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
require File.join 'mpatch','module'
|
59
|
+
extend MPatch::Include::Module
|
60
|
+
#@@valid_inject_cmd= true
|
61
|
+
|
62
|
+
end
|
data/lib/mpatch/integer.rb
CHANGED
@@ -1,14 +1,22 @@
|
|
1
|
+
module MPatch
|
1
2
|
|
2
|
-
module
|
3
|
-
module Integer
|
3
|
+
module Include
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
module Integer
|
6
|
+
|
7
|
+
# because for i in integer/fixnum not working,
|
8
|
+
# here is a little patch
|
9
|
+
def each &block
|
10
|
+
self.times do |number|
|
11
|
+
block.call number
|
12
|
+
end
|
10
13
|
end
|
14
|
+
|
11
15
|
end
|
12
16
|
|
13
17
|
end
|
18
|
+
|
19
|
+
require File.join 'mpatch','injector'
|
20
|
+
|
21
|
+
|
14
22
|
end
|
data/lib/mpatch/module.rb
CHANGED
@@ -1,77 +1,88 @@
|
|
1
|
-
module MPatch
|
2
|
-
module Module
|
1
|
+
module MPatch
|
3
2
|
|
4
|
-
|
5
|
-
def submodules
|
6
|
-
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Module}
|
7
|
-
end
|
3
|
+
module Include
|
8
4
|
|
9
|
-
|
10
|
-
def subclasses
|
11
|
-
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Class}
|
12
|
-
end
|
5
|
+
module Module
|
13
6
|
|
14
|
-
|
15
|
-
|
7
|
+
# return the module objects direct sub modules
|
8
|
+
def submodules
|
9
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Module}
|
10
|
+
end
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
12
|
+
# return the module objects direct sub modules
|
13
|
+
def subclasses
|
14
|
+
constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == ::Class}
|
15
|
+
end
|
21
16
|
|
22
|
-
|
17
|
+
alias :modules :submodules
|
18
|
+
alias :classes :subclasses
|
23
19
|
|
24
|
-
|
25
|
-
|
26
|
-
|
20
|
+
def mixin_ancestors(include_ancestors=true)
|
21
|
+
ancestors.take_while {|a| include_ancestors || a != superclass }.
|
22
|
+
select {|ancestor| ancestor.instance_of?( ::Module ) }
|
27
23
|
end
|
28
24
|
|
29
|
-
|
30
|
-
|
25
|
+
def inherited_by *args
|
26
|
+
|
27
|
+
if args.empty?
|
28
|
+
args.push(::Class)
|
29
|
+
args.push(::Module)
|
30
|
+
end
|
31
|
+
|
32
|
+
return_array= []
|
33
|
+
args.each do |type_name|
|
34
|
+
|
35
|
+
::ObjectSpace.each_object(type_name) do |candidate|
|
36
|
+
begin
|
31
37
|
|
32
|
-
|
33
|
-
|
38
|
+
if !return_array.include?(candidate) && candidate != self
|
39
|
+
case self.class.to_s
|
34
40
|
|
35
|
-
|
36
|
-
|
41
|
+
when "Module"
|
42
|
+
return_array.push candidate if candidate.mixin_ancestors.include?(self)
|
37
43
|
|
38
|
-
|
39
|
-
|
44
|
+
when "Class"
|
45
|
+
return_array.push candidate if candidate < self
|
40
46
|
|
41
|
-
|
42
|
-
return_array.push candidate if candidate < self
|
47
|
+
end
|
43
48
|
|
44
49
|
end
|
45
50
|
|
51
|
+
rescue ::ArgumentError, ::NoMethodError
|
46
52
|
end
|
47
|
-
|
48
|
-
rescue ::ArgumentError, ::NoMethodError
|
49
53
|
end
|
54
|
+
|
50
55
|
end
|
56
|
+
return_array
|
51
57
|
|
52
58
|
end
|
53
|
-
return_array
|
54
59
|
|
55
|
-
end
|
56
60
|
|
61
|
+
end
|
57
62
|
|
58
63
|
end
|
59
|
-
end
|
60
64
|
|
61
|
-
module
|
62
|
-
|
65
|
+
module Extend
|
66
|
+
|
67
|
+
module Module
|
63
68
|
|
64
|
-
|
69
|
+
def convert_instance_methods_to_singleton_methods
|
70
|
+
|
71
|
+
self.instance_methods.each do |symbol|
|
72
|
+
module_function symbol
|
73
|
+
public symbol
|
74
|
+
end
|
65
75
|
|
66
|
-
self.instance_methods.each do |symbol|
|
67
|
-
module_function symbol
|
68
|
-
public symbol
|
69
76
|
end
|
70
77
|
|
71
|
-
|
78
|
+
alias :ci2s :convert_instance_methods_to_singleton_methods
|
79
|
+
alias :instances2singletons :convert_instance_methods_to_singleton_methods
|
72
80
|
|
73
|
-
|
74
|
-
alias :instances2singletons :convert_instance_methods_to_singleton_methods
|
81
|
+
end
|
75
82
|
|
76
83
|
end
|
77
|
-
|
84
|
+
|
85
|
+
require File.join 'mpatch','injector'
|
86
|
+
|
87
|
+
|
88
|
+
end
|