mpatch 2.6.0 → 2.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +45 -1
- data/VERSION +1 -1
- data/examples/privatize.rb +25 -0
- data/lib/mpatch/object.rb +132 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7dd7c17f48dc461fca6ece53d32cce849160b5b1
|
4
|
+
data.tar.gz: 0115b19819852989358263809c9ae6fc77edf6c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76d9ef3cc93a73b25399548a5e1e35dc0d518ee23d76b63fa235899f0dde3b108e7271ae8780cbc298a11b79218cdfaaf7948539ce14df08fd190fdf357632ea
|
7
|
+
data.tar.gz: fe8d21cb3ae238ef7a5290ab0063bee2fa2bee9e23a78e310ccd6cb14e53606e65594c3dbaff42a7e9f0e7ccf7d6ee47ca62133c62e9d3bbcb4e1d8fd719bc13
|
data/README.md
CHANGED
@@ -18,7 +18,6 @@ This will result in lot of helper methods, and metaprograming tricks
|
|
18
18
|
require 'mpatch'
|
19
19
|
```
|
20
20
|
|
21
|
-
|
22
21
|
### example with Class methods
|
23
22
|
|
24
23
|
```ruby
|
@@ -123,6 +122,51 @@ you can make your own monkey patches by the following
|
|
123
122
|
# if you want to contribute with use cases, please do so! :)
|
124
123
|
```
|
125
124
|
|
125
|
+
### Fun with access rights
|
126
|
+
|
127
|
+
you can make an object / class / module instance or self methods private even from an instance
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
|
131
|
+
# this is used for make private methods in an object
|
132
|
+
# you can also use this to convert already defined methods in an object or class
|
133
|
+
# use:
|
134
|
+
#
|
135
|
+
# privatize in: 'hello_world' #> make hello world method private in the self obj
|
136
|
+
#
|
137
|
+
# privatize target: 'instance' #> you can use this in a class to make instance methods private
|
138
|
+
# privatize target: 'singleton' #> you can use this in a class to make self methods private
|
139
|
+
#
|
140
|
+
# privatize ex: Symbol/String/Array #> you can use this for make exceptions what should be not touched in the prcs
|
141
|
+
# privatize in: Symbol/String/Array #> you can use this for make targeted collection of methods for privatize
|
142
|
+
#
|
143
|
+
|
144
|
+
class Test
|
145
|
+
|
146
|
+
def say
|
147
|
+
"hello world"
|
148
|
+
end
|
149
|
+
|
150
|
+
def sup
|
151
|
+
"fine thx"
|
152
|
+
end
|
153
|
+
|
154
|
+
privatize t: :instance, ex: 'sup'
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
test1= Test.new
|
159
|
+
|
160
|
+
puts test1.sup #> fine thx
|
161
|
+
|
162
|
+
puts try{ test1.say } #> will fail and say error instead "hello world"
|
163
|
+
puts test1.__send__ :say #> hello world
|
164
|
+
|
165
|
+
test1.privatize only: 'sup'
|
166
|
+
puts try{ test1.sup } #> fail again because it's private
|
167
|
+
|
168
|
+
```
|
169
|
+
|
126
170
|
But there is a lot of method, for example for modules modules / subbmodules call that retunr modules under that namespace.
|
127
171
|
Lot of metaprogrammer stuff there too :)
|
128
172
|
|
data/VERSION
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
2.
|
1
|
+
2.7.0
|
2
2
|
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'mpatch'
|
2
|
+
|
3
|
+
class Test
|
4
|
+
|
5
|
+
def say
|
6
|
+
"hello world"
|
7
|
+
end
|
8
|
+
|
9
|
+
def sup
|
10
|
+
"fine thx"
|
11
|
+
end
|
12
|
+
|
13
|
+
privatize t: :instance, ex: 'sup'
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
test1= Test.new
|
18
|
+
|
19
|
+
puts test1.sup #> fine thx
|
20
|
+
|
21
|
+
puts try{ test1.say } #> will fail and say error instead "hello world"
|
22
|
+
puts test1.__send__ :say #> hello world
|
23
|
+
|
24
|
+
test1.privatize only: 'sup'
|
25
|
+
puts try{ test1.sup } #> fail again because it's private
|
data/lib/mpatch/object.rb
CHANGED
@@ -165,6 +165,138 @@ module MPatch
|
|
165
165
|
|
166
166
|
alias :create_attributes :convert_to_class
|
167
167
|
|
168
|
+
# this is used for make private methods in an object
|
169
|
+
# you can also use this to convert already defined methods in an object or class
|
170
|
+
# use:
|
171
|
+
#
|
172
|
+
# privatize in: 'hello_world' #> make hello world method private in the self obj
|
173
|
+
#
|
174
|
+
# privatize target: 'instance' #> you can use this in a class to make instance methods private
|
175
|
+
# privatize target: 'singleton' #> you can use this in a class to make self methods private
|
176
|
+
#
|
177
|
+
# privatize ex: Symbol/String/Array #> you can use this for make exceptions what should be not touched in the prcs
|
178
|
+
# privatize in: Symbol/String/Array #> you can use this for make targeted collection of methods for privatize
|
179
|
+
#
|
180
|
+
def privatize opts= {}
|
181
|
+
|
182
|
+
unless opts.class <= Hash
|
183
|
+
raise ArgumentError,"invalid input for options"
|
184
|
+
end
|
185
|
+
|
186
|
+
%W[ e ex exc ].each do |str_sym|
|
187
|
+
opts[:exclude] ||= opts[str_sym.to_sym]
|
188
|
+
end
|
189
|
+
|
190
|
+
%W[ i in inc only methods ].each do |str_sym|
|
191
|
+
opts[:include] ||= opts[str_sym.to_sym]
|
192
|
+
end
|
193
|
+
|
194
|
+
%W[ t target ].each do |str_sym|
|
195
|
+
opts[:target] ||= opts[str_sym.to_sym]
|
196
|
+
end
|
197
|
+
|
198
|
+
opts[:target] ||= 's'
|
199
|
+
opts[:target]= opts[:target].to_s.downcase
|
200
|
+
|
201
|
+
unless opts[:target][0] == "s" || opts[:target][0] == "i"
|
202
|
+
%W[ singleton instance ].include?(opts[:target].to_s)
|
203
|
+
raise ArgumentError, [
|
204
|
+
"invalid options for target, you should use the following: ",
|
205
|
+
"\n\tsingleton for targeting the singleton class what is de",
|
206
|
+
"fault\n\tinstance for targeting the object instance methods."
|
207
|
+
].join
|
208
|
+
end
|
209
|
+
|
210
|
+
opts[:exclude] ||= []
|
211
|
+
|
212
|
+
if opts[:target][0] == 'i' && self.class <= Module
|
213
|
+
opts[:include] ||= self.instance_methods.map{|e| e.to_s }
|
214
|
+
else
|
215
|
+
opts[:target]= 's'
|
216
|
+
opts[:include] ||= self.methods.map{|e| e.to_s }
|
217
|
+
|
218
|
+
end
|
219
|
+
|
220
|
+
[:include,:exclude].each do |array_name|
|
221
|
+
|
222
|
+
unless opts[array_name].class <= Array
|
223
|
+
opts[array_name]= [ opts[array_name] ]
|
224
|
+
end
|
225
|
+
|
226
|
+
opts[array_name].map!{ |element| ( element.class == String ? element : element.to_s ) }
|
227
|
+
|
228
|
+
end
|
229
|
+
|
230
|
+
opts[:exclude].push('__send__').push('object_id')
|
231
|
+
|
232
|
+
if opts[:target][0] == 's'
|
233
|
+
|
234
|
+
self.instance_eval do
|
235
|
+
|
236
|
+
opts[:include].each do |sym|
|
237
|
+
|
238
|
+
unless opts[:exclude].include?(sym)
|
239
|
+
metaclass.__send__ :private, sym
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
elsif opts[:target][0] == 'i'
|
246
|
+
|
247
|
+
opts[:include].each do |sym|
|
248
|
+
|
249
|
+
unless opts[:exclude].include?(sym)
|
250
|
+
self.__send__ :private, sym
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
else
|
256
|
+
STDERR.puts "invalid target definition"
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
end
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
def try(exception= Exception,&block)
|
265
|
+
|
266
|
+
unless exception <= Exception
|
267
|
+
raise ArgumentError, "invalid exception class"
|
268
|
+
end
|
269
|
+
|
270
|
+
begin
|
271
|
+
return block.call
|
272
|
+
rescue exception => ex
|
273
|
+
@ruby_try_block_exception_tunel= ex
|
274
|
+
return ex
|
275
|
+
end
|
276
|
+
|
277
|
+
end
|
278
|
+
|
279
|
+
def catch exception = Exception, &block
|
280
|
+
|
281
|
+
if @ruby_try_block_exception_tunel.nil?
|
282
|
+
return nil
|
283
|
+
end
|
284
|
+
|
285
|
+
if @ruby_try_block_exception_tunel.class <= Exception
|
286
|
+
|
287
|
+
begin
|
288
|
+
block.call @ruby_try_block_exception_tunel
|
289
|
+
rescue ArgumentError
|
290
|
+
block.call
|
291
|
+
end
|
292
|
+
|
293
|
+
else
|
294
|
+
raise @ruby_try_block_exception_tunel.class,@ruby_try_block_exception_tunel.to_s
|
295
|
+
end
|
296
|
+
|
297
|
+
end
|
298
|
+
|
299
|
+
|
168
300
|
end
|
169
301
|
|
170
302
|
end
|
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.7.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-04-
|
11
|
+
date: 2014-04-10 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
|
@@ -24,6 +24,7 @@ files:
|
|
24
24
|
- VERSION
|
25
25
|
- create_folder.rb
|
26
26
|
- dump/class_and_module.rb
|
27
|
+
- examples/privatize.rb
|
27
28
|
- examples/test.rb
|
28
29
|
- files.rb
|
29
30
|
- lib/mpatch.rb
|