mpatch 2.9.0 → 2.11.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 +18 -4
- data/VERSION +1 -1
- data/examples/class.rb +25 -0
- data/examples/hash.rb +17 -0
- data/lib/mpatch.rb +0 -2
- data/lib/mpatch/array.rb +36 -0
- data/lib/mpatch/hash.rb +24 -8
- data/lib/mpatch/module.rb +24 -6
- data/lib/mpatch/object.rb +3 -3
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cec135221ac3f2a5c694c8d93fe4a8765e3bca68
|
4
|
+
data.tar.gz: ac1e24c549d1c98cff57023453529513838a4efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2bd6ff235e20003cc427d16a8f3efd475bd2d250ca3921373cad8e90ca421c6ce3c883755f8b5cf138cbcffb08aa0dff68a0f0cd186a301250e06580d8f9e1f
|
7
|
+
data.tar.gz: 7a5d7383f5ea92faddf35238a180f6b978c29c644766dd39d936b3331695dae9101343ec9179c7a2bce667c05aef25cf35041d7a0ec7098ded7b4bb04024c3c0
|
data/README.md
CHANGED
@@ -21,27 +21,41 @@ This will result in lot of helper methods, and metaprograming tricks
|
|
21
21
|
### example with Class methods
|
22
22
|
|
23
23
|
```ruby
|
24
|
+
|
24
25
|
require 'mpatch'
|
25
26
|
|
26
|
-
class
|
27
|
+
class Test2
|
28
|
+
@@test= "asd"
|
29
|
+
end
|
30
|
+
|
31
|
+
class Test < Test2
|
27
32
|
|
28
33
|
def initialize
|
29
34
|
|
30
35
|
@hello= "world"
|
31
|
-
@
|
36
|
+
@no = "yes"
|
32
37
|
|
33
38
|
end
|
34
39
|
|
35
40
|
end
|
36
41
|
|
37
|
-
puts
|
38
|
-
|
42
|
+
puts Test2.inherited_by.inspect
|
43
|
+
# [Test]
|
44
|
+
|
45
|
+
puts Test.conv2hash
|
46
|
+
# {"test"=>"asd"}
|
47
|
+
|
48
|
+
puts Test.new.conv2hash
|
49
|
+
# {"hello"=>"world", "no"=>"yes"}
|
50
|
+
|
51
|
+
|
39
52
|
```
|
40
53
|
|
41
54
|
The module give you tools in your hand for inheritance handle.
|
42
55
|
For example:
|
43
56
|
|
44
57
|
```ruby
|
58
|
+
|
45
59
|
puts Models::MongoidClassName.mixin_ancestors.include? Mongoid::Document
|
46
60
|
#> true
|
47
61
|
|
data/VERSION
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
2.
|
1
|
+
2.11.0
|
2
2
|
|
data/examples/class.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mpatch'
|
2
|
+
|
3
|
+
class Test2
|
4
|
+
@@test= "asd"
|
5
|
+
end
|
6
|
+
|
7
|
+
class Test < Test2
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
|
11
|
+
@hello= "world"
|
12
|
+
@no = "yes"
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
puts Test2.inherited_by.inspect
|
19
|
+
# [Test]
|
20
|
+
|
21
|
+
puts Test.conv2hash
|
22
|
+
# {"test"=>"asd"}
|
23
|
+
|
24
|
+
puts Test.new.conv2hash
|
25
|
+
# {"hello"=>"world", "no"=>"yes"}
|
data/examples/hash.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
require "mpatch"
|
3
|
+
|
4
|
+
x = {}
|
5
|
+
(1..10000).each do |i|
|
6
|
+
x["key#{i}"] = i
|
7
|
+
end
|
8
|
+
|
9
|
+
t=Time.now
|
10
|
+
x.map_hash{ |k,v| { k.to_sym => v.to_s } }
|
11
|
+
puts Time.now - t
|
12
|
+
|
13
|
+
var= {hello: "world",no: "yes"}
|
14
|
+
puts var
|
15
|
+
|
16
|
+
var= [[:hello, "world"],[:no, "yes"]].map_hash{|k,v| {k => v} }
|
17
|
+
puts var.inspect
|
data/lib/mpatch.rb
CHANGED
data/lib/mpatch/array.rb
CHANGED
@@ -168,6 +168,42 @@ module MPatch
|
|
168
168
|
end
|
169
169
|
alias :extract_hash! :extract_options!
|
170
170
|
|
171
|
+
# map hash will work just alike map but instead of an array it will return a hash obj
|
172
|
+
#
|
173
|
+
# [:hello, "world",:world , "hello"].map_hash{|k,v| [ k , 123] }
|
174
|
+
# #> {:hello=>123, :world=>123}
|
175
|
+
#
|
176
|
+
# [:hello,"world",:world,"hello"].map_hash{|k,v| { k => 123 } }
|
177
|
+
# #> {:hello=>123, :world=>123}
|
178
|
+
#
|
179
|
+
# [[:hello,"world"],[:world,"hello"]].map_hash{ |ary| { ary[0] => ary[1] } }
|
180
|
+
# #> {:hello=>"world", :world=>"hello"}
|
181
|
+
#
|
182
|
+
def map_hash *args,&block
|
183
|
+
|
184
|
+
tmp_hash= ::Hash.new(*args)
|
185
|
+
self.map(&block).each do |hash|
|
186
|
+
case true
|
187
|
+
|
188
|
+
when hash.class <= ::Array
|
189
|
+
tmp_hash.deep_merge!(::Hash[*hash])
|
190
|
+
|
191
|
+
when hash.class <= ::Hash
|
192
|
+
tmp_hash.deep_merge!(hash)
|
193
|
+
|
194
|
+
else
|
195
|
+
raise ArgumentError,
|
196
|
+
"invalid input as last valie for #{__method__}: #{hash}/#{hash.class}"
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
return tmp_hash
|
203
|
+
|
204
|
+
end
|
205
|
+
alias :map2hash :map_hash
|
206
|
+
|
171
207
|
end
|
172
208
|
|
173
209
|
end
|
data/lib/mpatch/hash.rb
CHANGED
@@ -93,23 +93,39 @@ module MPatch
|
|
93
93
|
# {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
|
94
94
|
# #> {:hello=>123, :world=>123}
|
95
95
|
#
|
96
|
-
def map_hash
|
96
|
+
def map_hash *args,&block
|
97
97
|
|
98
|
-
tmp_hash= self.class.new
|
99
|
-
|
100
|
-
map_hash_obj.each do |hash|
|
98
|
+
tmp_hash= self.class.new(*args)
|
99
|
+
self.map(&block).each do |hash|
|
101
100
|
|
102
|
-
|
103
|
-
|
104
|
-
|
101
|
+
case true
|
102
|
+
|
103
|
+
when hash.class <= ::Array
|
104
|
+
tmp_hash.deep_merge!( self.class[*hash] )
|
105
105
|
|
106
|
-
|
106
|
+
when hash.class <= ::Hash
|
107
|
+
tmp_hash.deep_merge!(hash)
|
108
|
+
|
109
|
+
else
|
110
|
+
raise ArgumentError,
|
111
|
+
"invalid input as last valie for #{__method__}: #{hash}/#{hash.class}"
|
112
|
+
|
113
|
+
end
|
107
114
|
|
108
115
|
end
|
109
116
|
|
110
117
|
return tmp_hash
|
118
|
+
|
119
|
+
end
|
120
|
+
alias :map2hash :map_hash
|
121
|
+
|
122
|
+
def map_hash! *args,&block
|
123
|
+
self.replace(self.map_hash(*args,&block))
|
111
124
|
end
|
112
125
|
|
126
|
+
alias :map2hash! :map_hash!
|
127
|
+
|
128
|
+
|
113
129
|
# Fetch a nested hash value
|
114
130
|
def value_by_keys(*attrs)
|
115
131
|
attr_count = attrs.size
|
data/lib/mpatch/module.rb
CHANGED
@@ -36,12 +36,14 @@ module MPatch
|
|
36
36
|
begin
|
37
37
|
|
38
38
|
if !return_array.include?(candidate) && candidate != self
|
39
|
-
case self.class.to_s
|
40
39
|
|
41
|
-
|
40
|
+
#> can't make subclass of Class so == is enough
|
41
|
+
case true
|
42
|
+
|
43
|
+
when self.class == ::Module
|
42
44
|
return_array.push candidate if candidate.mixin_ancestors.include?(self)
|
43
45
|
|
44
|
-
when
|
46
|
+
when self.class == ::Class
|
45
47
|
return_array.push candidate if candidate < self
|
46
48
|
|
47
49
|
end
|
@@ -49,11 +51,12 @@ module MPatch
|
|
49
51
|
end
|
50
52
|
|
51
53
|
rescue ::ArgumentError, ::NoMethodError
|
54
|
+
|
52
55
|
end
|
53
56
|
end
|
54
57
|
|
55
58
|
end
|
56
|
-
return_array
|
59
|
+
return return_array
|
57
60
|
|
58
61
|
end
|
59
62
|
|
@@ -88,6 +91,23 @@ module MPatch
|
|
88
91
|
|
89
92
|
end
|
90
93
|
|
94
|
+
def hello
|
95
|
+
"hello"
|
96
|
+
end
|
97
|
+
|
98
|
+
#convert class instance instance variables into a hash object
|
99
|
+
def convert_to_hash
|
100
|
+
|
101
|
+
tmp_hash= {}
|
102
|
+
self.class_variables.each do|var|
|
103
|
+
tmp_hash[var.to_s.delete("@@")] = self.class_variable_get(var)
|
104
|
+
end
|
105
|
+
|
106
|
+
return tmp_hash
|
107
|
+
|
108
|
+
end
|
109
|
+
alias :conv2hash :convert_to_hash
|
110
|
+
|
91
111
|
end
|
92
112
|
|
93
113
|
end
|
@@ -104,8 +124,6 @@ module MPatch
|
|
104
124
|
end
|
105
125
|
|
106
126
|
end
|
107
|
-
|
108
|
-
alias :ci2s :convert_instance_methods_to_singleton_methods
|
109
127
|
alias :instances2singletons :convert_instance_methods_to_singleton_methods
|
110
128
|
|
111
129
|
end
|
data/lib/mpatch/object.rb
CHANGED
@@ -66,6 +66,7 @@ module MPatch
|
|
66
66
|
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
67
67
|
end
|
68
68
|
constant
|
69
|
+
|
69
70
|
end
|
70
71
|
|
71
72
|
#convert class instance instance variables into a hash object
|
@@ -73,11 +74,9 @@ module MPatch
|
|
73
74
|
|
74
75
|
unless self.class.class <= ::Class
|
75
76
|
super
|
76
|
-
#raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
|
77
77
|
end
|
78
78
|
|
79
|
-
tmp_hash=
|
80
|
-
|
79
|
+
tmp_hash= {}
|
81
80
|
self.instance_variables.each do|var|
|
82
81
|
tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
|
83
82
|
end
|
@@ -85,6 +84,7 @@ module MPatch
|
|
85
84
|
return tmp_hash
|
86
85
|
|
87
86
|
end
|
87
|
+
alias :conv2hash :convert_to_hash
|
88
88
|
|
89
89
|
# this will check that the class is
|
90
90
|
# defined or not in the runtime memory
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mpatch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Luzsi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This is a collection of my Ruby monkey patches for making easer to use
|
14
14
|
the basic classes
|
@@ -25,6 +25,8 @@ files:
|
|
25
25
|
- create_folder.rb
|
26
26
|
- dump/class_and_module.rb
|
27
27
|
- examples/array.rb
|
28
|
+
- examples/class.rb
|
29
|
+
- examples/hash.rb
|
28
30
|
- examples/privatize.rb
|
29
31
|
- examples/test.rb
|
30
32
|
- files.rb
|