mpatch 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mpatch/hash.rb CHANGED
@@ -1,111 +1,120 @@
1
- module MPatch::Include
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)
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
- when one_element.class <= ::Array
23
- begin
24
- one_element.each do |one_key|
25
- self.delete(one_key)
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
- when one_element.class <= ::Symbol,
30
- one_element.class <= ::String
31
- begin
32
- self.delete(one_element)
33
- end
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
- end
37
- return self
38
-
39
- end
40
+ return self
40
41
 
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
45
- end
46
-
47
- #non-destructive version
48
- def remove(*keys)
49
- self.dup.remove!(*keys)
50
- end
42
+ end
51
43
 
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
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
- 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
49
+
50
+ #non-destructive version
51
+ def remove(*keys)
52
+ self.dup.remove!(*keys)
82
53
  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|
98
-
99
- if hash.class <= ::Array
100
- hash= self.class[*hash]
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
- tmp_hash.deep_merge!(hash)
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
@@ -1,14 +1,22 @@
1
+ module MPatch
1
2
 
2
- module MPatch::Include
3
- module Integer
3
+ module Include
4
4
 
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
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::Include
2
- module Module
1
+ module MPatch
3
2
 
4
- # return the module objects direct sub modules
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
- # return the module objects direct sub modules
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
- alias :modules :submodules
15
- alias :classes :subclasses
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
- def mixin_ancestors(include_ancestors=true)
18
- ancestors.take_while {|a| include_ancestors || a != superclass }.
19
- select {|ancestor| ancestor.instance_of?( ::Module ) }
20
- end
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
- def inherited_by *args
17
+ alias :modules :submodules
18
+ alias :classes :subclasses
23
19
 
24
- if args.empty?
25
- args.push(::Class)
26
- args.push(::Module)
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
- return_array= []
30
- args.each do |type_name|
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
- ::ObjectSpace.each_object(type_name) do |candidate|
33
- begin
38
+ if !return_array.include?(candidate) && candidate != self
39
+ case self.class.to_s
34
40
 
35
- if !return_array.include?(candidate) && candidate != self
36
- case self.class.to_s
41
+ when "Module"
42
+ return_array.push candidate if candidate.mixin_ancestors.include?(self)
37
43
 
38
- when "Module"
39
- return_array.push candidate if candidate.mixin_ancestors.include?(self)
44
+ when "Class"
45
+ return_array.push candidate if candidate < self
40
46
 
41
- when "Class"
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 MPatch::Extend
62
- module Module
65
+ module Extend
66
+
67
+ module Module
63
68
 
64
- def convert_instance_methods_to_singleton_methods
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
- end
78
+ alias :ci2s :convert_instance_methods_to_singleton_methods
79
+ alias :instances2singletons :convert_instance_methods_to_singleton_methods
72
80
 
73
- alias :ci2s :convert_instance_methods_to_singleton_methods
74
- alias :instances2singletons :convert_instance_methods_to_singleton_methods
81
+ end
75
82
 
76
83
  end
77
- end
84
+
85
+ require File.join 'mpatch','injector'
86
+
87
+
88
+ end