mpatch 1.3.0 → 2.0.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.
data/lib/mpatch/hash.rb CHANGED
@@ -1,109 +1,111 @@
1
- class Hash
2
-
3
- # remove elements by keys,
4
- # array of keys,
5
- # hashTags,
6
- # strings
7
- def trim(*args)
8
-
9
- args.each do |one_element|
10
- case true
11
-
12
- when one_element.class == Hash
13
- begin
14
- one_element.each do |key,value|
15
- if self[key] == value
16
- self.delete(key)
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
- when one_element.class == Array
22
- begin
23
- one_element.each do |one_key|
24
- self.delete(one_key)
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
- when one_element.class == Symbol,
29
- one_element.class == String
30
- begin
31
- self.delete(one_element)
32
- end
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
- end
36
- return self
37
-
38
- end
37
+ return self
39
38
 
40
- #pass single or array of keys, which will be removed, returning the remaining hash
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
- # Returns a new hash with +self+ and +other_hash+ merged recursively.
52
- #
53
- # h1 = {:x => {:y => [4,5,6]}, :z => [7,8,9]}
54
- # h2 = {:x => {:y => [7,8,9]}, :z => "xyz"}
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
- self
71
- end unless method_defined? :deep_merge!
72
-
73
- # return bool that does the sub hash all element include the hash who call this or not
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
- end unless method_defined? :deep_include?
83
-
84
- # map hash will work just alike map but instead of an array it will return a hash obj
85
- #
86
- # {:hello=> "world",:world => "hello"}.map_hash{|k,v| [ k , 123] }
87
- # #> {:hello=>123, :world=>123}
88
- #
89
- # {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
90
- # #> {:hello=>123, :world=>123}
91
- #
92
- def map_hash &block
93
-
94
- tmp_hash= Hash.new
95
- map_hash_obj= self.map &block
96
- map_hash_obj.each do |hash|
97
-
98
- if hash.class <= Array
99
- hash= Hash[*hash]
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
- tmp_hash.deep_merge!(hash)
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
@@ -1,12 +1,14 @@
1
1
 
2
- class Integer
2
+ module MPatch
3
+ module Integer
3
4
 
4
- # because for i in integer/fixnum not working,
5
- # here is a little patch
6
- def each &block
7
- self.times do |number|
8
- block.call number
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
- class Module
1
+ module MPatch
2
+ class Module
2
3
 
3
- # return the module objects direct sub modules
4
- def modules
5
- constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Module}
6
- end
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
- # return the module objects direct sub modules
9
- def classes
10
- constants.collect {|const_name| const_get(const_name)}.select {|const| const.class == Class}
11
- end
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
- alias :submodules :modules
14
- alias :subclasses :classes
14
+ alias :submodules :modules
15
+ alias :subclasses :classes
15
16
 
17
+ end
16
18
  end