knjrbfw 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/VERSION +1 -1
  2. data/knjrbfw.gemspec +7 -4
  3. data/lib/knj/autoload.rb +1 -61
  4. data/lib/knj/datarow.rb +7 -10
  5. data/lib/knj/datarow_custom.rb +12 -2
  6. data/lib/knj/datet.rb +107 -0
  7. data/lib/knj/eruby.rb +21 -12
  8. data/lib/knj/gettext_threadded.rb +1 -1
  9. data/lib/knj/http2.rb +27 -9
  10. data/lib/knj/image.rb +10 -0
  11. data/lib/knj/includes/require_info.rb +3 -3
  12. data/lib/knj/knj.rb +16 -9
  13. data/lib/knj/knj_controller.rb +10 -1
  14. data/lib/knj/knjdb/drivers/mysql/knjdb_mysql.rb +26 -9
  15. data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_columns.rb +11 -8
  16. data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_indexes.rb +5 -0
  17. data/lib/knj/knjdb/drivers/mysql/knjdb_mysql_tables.rb +83 -26
  18. data/lib/knj/knjdb/drivers/sqlite3/knjdb_sqlite3_tables.rb +3 -3
  19. data/lib/knj/knjdb/libknjdb.rb +19 -20
  20. data/lib/knj/knjdb/revision.rb +9 -2
  21. data/lib/knj/kvm.rb +100 -0
  22. data/lib/knj/locale_strings.rb +32 -3
  23. data/lib/knj/locales.rb +1 -4
  24. data/lib/knj/memory_analyzer.rb +335 -0
  25. data/lib/knj/objects/objects_sqlhelper.rb +2 -2
  26. data/lib/knj/objects.rb +44 -11
  27. data/lib/knj/opts.rb +8 -8
  28. data/lib/knj/os.rb +13 -0
  29. data/lib/knj/php.rb +18 -33
  30. data/lib/knj/process.rb +3 -0
  31. data/lib/knj/process_meta.rb +1 -1
  32. data/lib/knj/rhodes/mutex.rb +2 -1
  33. data/lib/knj/rhodes/rhodes.js +5 -1
  34. data/lib/knj/rhodes/rhodes.rb +15 -15
  35. data/lib/knj/rhodes/youtube_embed.erb +12 -0
  36. data/lib/knj/rhodes/youtube_open.erb +45 -0
  37. data/lib/knj/strings.rb +2 -1
  38. data/lib/knj/translations.rb +3 -3
  39. data/lib/knj/unix_proc.rb +15 -4
  40. data/lib/knj/web.rb +17 -247
  41. data/lib/knj/webscripts/image.rhtml +9 -3
  42. data/lib/knj/wref.rb +109 -70
  43. data/spec/datet_spec.rb +30 -0
  44. data/spec/http2_spec.rb +23 -0
  45. data/spec/php_spec.rb +3 -0
  46. metadata +20 -17
  47. data/lib/knj/rhodes/delegate.rb +0 -414
  48. data/lib/knj/rhodes/weakref.rb +0 -80
@@ -1,414 +0,0 @@
1
- # = delegate -- Support for the Delegation Pattern
2
- #
3
- # Documentation by James Edward Gray II and Gavin Sinclair
4
- #
5
- # == Introduction
6
- #
7
- # This library provides three different ways to delegate method calls to an
8
- # object. The easiest to use is SimpleDelegator. Pass an object to the
9
- # constructor and all methods supported by the object will be delegated. This
10
- # object can be changed later.
11
- #
12
- # Going a step further, the top level DelegateClass method allows you to easily
13
- # setup delegation through class inheritance. This is considerably more
14
- # flexible and thus probably the most common use for this library.
15
- #
16
- # Finally, if you need full control over the delegation scheme, you can inherit
17
- # from the abstract class Delegator and customize as needed. (If you find
18
- # yourself needing this control, have a look at _forwardable_, also in the
19
- # standard library. It may suit your needs better.)
20
- #
21
- # == Notes
22
- #
23
- # Be advised, RDoc will not detect delegated methods.
24
- #
25
- # <b>delegate.rb provides full-class delegation via the
26
- # DelegateClass() method. For single-method delegation via
27
- # def_delegator(), see forwardable.rb.</b>
28
- #
29
- # == Examples
30
- #
31
- # === SimpleDelegator
32
- #
33
- # Here's a simple example that takes advantage of the fact that
34
- # SimpleDelegator's delegation object can be changed at any time.
35
- #
36
- # class Stats
37
- # def initialize
38
- # @source = SimpleDelegator.new([])
39
- # end
40
- #
41
- # def stats( records )
42
- # @source.__setobj__(records)
43
- #
44
- # "Elements: #{@source.size}\n" +
45
- # " Non-Nil: #{@source.compact.size}\n" +
46
- # " Unique: #{@source.uniq.size}\n"
47
- # end
48
- # end
49
- #
50
- # s = Stats.new
51
- # puts s.stats(%w{James Edward Gray II})
52
- # puts
53
- # puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
54
- #
55
- # <i>Prints:</i>
56
- #
57
- # Elements: 4
58
- # Non-Nil: 4
59
- # Unique: 4
60
- #
61
- # Elements: 8
62
- # Non-Nil: 7
63
- # Unique: 6
64
- #
65
- # === DelegateClass()
66
- #
67
- # Here's a sample of use from <i>tempfile.rb</i>.
68
- #
69
- # A _Tempfile_ object is really just a _File_ object with a few special rules
70
- # about storage location and/or when the File should be deleted. That makes for
71
- # an almost textbook perfect example of how to use delegation.
72
- #
73
- # class Tempfile < DelegateClass(File)
74
- # # constant and class member data initialization...
75
- #
76
- # def initialize(basename, tmpdir=Dir::tmpdir)
77
- # # build up file path/name in var tmpname...
78
- #
79
- # @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
80
- #
81
- # # ...
82
- #
83
- # super(@tmpfile)
84
- #
85
- # # below this point, all methods of File are supported...
86
- # end
87
- #
88
- # # ...
89
- # end
90
- #
91
- # === Delegator
92
- #
93
- # SimpleDelegator's implementation serves as a nice example here.
94
- #
95
- # class SimpleDelegator < Delegator
96
- # def initialize(obj)
97
- # super # pass obj to Delegator constructor, required
98
- # @delegate_sd_obj = obj # store obj for future use
99
- # end
100
- #
101
- # def __getobj__
102
- # @delegate_sd_obj # return object we are delegating to, required
103
- # end
104
- #
105
- # def __setobj__(obj)
106
- # @delegate_sd_obj = obj # change delegation object, a feature we're providing
107
- # end
108
- #
109
- # # ...
110
- # end
111
-
112
- #
113
- # Delegator is an abstract class used to build delegator pattern objects from
114
- # subclasses. Subclasses should redefine \_\_getobj\_\_. For a concrete
115
- # implementation, see SimpleDelegator.
116
- #
117
- class Delegator < BasicObject
118
- kernel = ::Kernel.dup
119
- kernel.class_eval do
120
- [:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
121
- undef_method m
122
- end
123
- end
124
- include kernel
125
-
126
- # :stopdoc:
127
- def self.const_missing(n)
128
- ::Object.const_get(n)
129
- end
130
- # :startdoc:
131
-
132
- #
133
- # Pass in the _obj_ to delegate method calls to. All methods supported by
134
- # _obj_ will be delegated to.
135
- #
136
- def initialize(obj)
137
- __setobj__(obj)
138
- end
139
-
140
- #
141
- # Handles the magic of delegation through \_\_getobj\_\_.
142
- #
143
- def method_missing(m, *args, &block)
144
- target = self.__getobj__
145
- begin
146
- target.respond_to?(m) ? target.__send__(m, *args, &block) : super(m, *args, &block)
147
- ensure
148
- $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
149
- end
150
- end
151
-
152
- #
153
- # Checks for a method provided by this the delegate object by forwarding the
154
- # call through \_\_getobj\_\_.
155
- #
156
- def respond_to_missing?(m, include_private)
157
- r = self.__getobj__.respond_to?(m, include_private)
158
- if r && include_private && !self.__getobj__.respond_to?(m, false)
159
- warn "#{caller(3)[0]}: delegator does not forward private method \##{m}"
160
- return false
161
- end
162
- r
163
- end
164
-
165
- #
166
- # Returns the methods available to this delegate object as the union
167
- # of this object's and \_\_getobj\_\_ methods.
168
- #
169
- def methods
170
- __getobj__.methods | super
171
- end
172
-
173
- #
174
- # Returns the methods available to this delegate object as the union
175
- # of this object's and \_\_getobj\_\_ public methods.
176
- #
177
- def public_methods(all=true)
178
- __getobj__.public_methods(all) | super
179
- end
180
-
181
- #
182
- # Returns the methods available to this delegate object as the union
183
- # of this object's and \_\_getobj\_\_ protected methods.
184
- #
185
- def protected_methods(all=true)
186
- __getobj__.protected_methods(all) | super
187
- end
188
-
189
- # Note: no need to specialize private_methods, since they are not forwarded
190
-
191
- #
192
- # Returns true if two objects are considered of equal value.
193
- #
194
- def ==(obj)
195
- return true if obj.equal?(self)
196
- self.__getobj__ == obj
197
- end
198
-
199
- #
200
- # Returns true if two objects are not considered of equal value.
201
- #
202
- def !=(obj)
203
- return false if obj.equal?(self)
204
- __getobj__ != obj
205
- end
206
-
207
- def !
208
- !__getobj__
209
- end
210
-
211
- #
212
- # This method must be overridden by subclasses and should return the object
213
- # method calls are being delegated to.
214
- #
215
- def __getobj__
216
- raise NotImplementedError, "need to define `__getobj__'"
217
- end
218
-
219
- #
220
- # This method must be overridden by subclasses and change the object delegate
221
- # to _obj_.
222
- #
223
- def __setobj__(obj)
224
- raise NotImplementedError, "need to define `__setobj__'"
225
- end
226
-
227
- #
228
- # Serialization support for the object returned by \_\_getobj\_\_.
229
- #
230
- def marshal_dump
231
- ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
232
- [
233
- :__v2__,
234
- ivars, ivars.map{|var| instance_variable_get(var)},
235
- __getobj__
236
- ]
237
- end
238
-
239
- #
240
- # Reinitializes delegation from a serialized object.
241
- #
242
- def marshal_load(data)
243
- version, vars, values, obj = data
244
- if version == :__v2__
245
- vars.each_with_index{|var, i| instance_variable_set(var, values[i])}
246
- __setobj__(obj)
247
- else
248
- __setobj__(data)
249
- end
250
- end
251
-
252
- def initialize_clone(obj) # :nodoc:
253
- self.__setobj__(obj.__getobj__.clone)
254
- end
255
- def initialize_dup(obj) # :nodoc:
256
- self.__setobj__(obj.__getobj__.dup)
257
- end
258
- private :initialize_clone, :initialize_dup
259
-
260
- ##
261
- # :method: trust
262
- # Trust both the object returned by \_\_getobj\_\_ and self.
263
- #
264
-
265
- ##
266
- # :method: untrust
267
- # Untrust both the object returned by \_\_getobj\_\_ and self.
268
- #
269
-
270
- ##
271
- # :method: taint
272
- # Taint both the object returned by \_\_getobj\_\_ and self.
273
- #
274
-
275
- ##
276
- # :method: untaint
277
- # Untaint both the object returned by \_\_getobj\_\_ and self.
278
- #
279
-
280
- [:trust, :untrust, :taint, :untaint].each do |method|
281
- define_method method do
282
- __getobj__.send(method)
283
- super()
284
- end
285
- end
286
-
287
- #
288
- # Freeze self and target at once.
289
- #
290
- def freeze
291
- __getobj__.freeze
292
- super
293
- end
294
-
295
- @delegator_api = self.public_instance_methods
296
- def self.public_api # :nodoc:
297
- @delegator_api
298
- end
299
- end
300
-
301
- #
302
- # A concrete implementation of Delegator, this class provides the means to
303
- # delegate all supported method calls to the object passed into the constructor
304
- # and even to change the object being delegated to at a later time with
305
- # \_\_setobj\_\_ .
306
- #
307
- class SimpleDelegator<Delegator
308
- # Returns the current object method calls are being delegated to.
309
- def __getobj__
310
- @delegate_sd_obj
311
- end
312
-
313
- #
314
- # Changes the delegate object to _obj_.
315
- #
316
- # It's important to note that this does *not* cause SimpleDelegator's methods
317
- # to change. Because of this, you probably only want to change delegation
318
- # to objects of the same type as the original delegate.
319
- #
320
- # Here's an example of changing the delegation object.
321
- #
322
- # names = SimpleDelegator.new(%w{James Edward Gray II})
323
- # puts names[1] # => Edward
324
- # names.__setobj__(%w{Gavin Sinclair})
325
- # puts names[1] # => Sinclair
326
- #
327
- def __setobj__(obj)
328
- raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
329
- @delegate_sd_obj = obj
330
- end
331
- end
332
-
333
- # :stopdoc:
334
- def Delegator.delegating_block(mid)
335
- lambda do |*args, &block|
336
- target = self.__getobj__
337
- begin
338
- target.__send__(mid, *args, &block)
339
- ensure
340
- $@.delete_if {|t| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o =~ t} if $@
341
- end
342
- end
343
- end
344
- # :startdoc:
345
-
346
- #
347
- # The primary interface to this library. Use to setup delegation when defining
348
- # your class.
349
- #
350
- # class MyClass < DelegateClass( ClassToDelegateTo ) # Step 1
351
- # def initialize
352
- # super(obj_of_ClassToDelegateTo) # Step 2
353
- # end
354
- # end
355
- #
356
- def DelegateClass(superclass)
357
- klass = Class.new(Delegator)
358
- methods = superclass.instance_methods
359
- methods -= ::Delegator.public_api
360
- methods -= [:to_s,:inspect,:=~,:!~,:===]
361
- klass.module_eval do
362
- def __getobj__ # :nodoc:
363
- @delegate_dc_obj
364
- end
365
- def __setobj__(obj) # :nodoc:
366
- raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
367
- @delegate_dc_obj = obj
368
- end
369
- methods.each do |method|
370
- define_method(method, Delegator.delegating_block(method))
371
- end
372
- end
373
- klass.define_singleton_method :public_instance_methods do |all=true|
374
- super(all) - superclass.protected_instance_methods
375
- end
376
- klass.define_singleton_method :protected_instance_methods do |all=true|
377
- super(all) | superclass.protected_instance_methods
378
- end
379
- return klass
380
- end
381
-
382
- # :enddoc:
383
-
384
- if __FILE__ == $0
385
- class ExtArray<DelegateClass(Array)
386
- def initialize()
387
- super([])
388
- end
389
- end
390
-
391
- ary = ExtArray.new
392
- p ary.class
393
- ary.push 25
394
- p ary
395
- ary.push 42
396
- ary.each {|x| p x}
397
-
398
- foo = Object.new
399
- def foo.test
400
- 25
401
- end
402
- def foo.iter
403
- yield self
404
- end
405
- def foo.error
406
- raise 'this is OK'
407
- end
408
- foo2 = SimpleDelegator.new(foo)
409
- p foo2
410
- foo2.instance_eval{print "foo\n"}
411
- p foo.test == foo2.test # => true
412
- p foo2.iter{[55,true]} # => true
413
- foo2.error # raise error!
414
- end
@@ -1,80 +0,0 @@
1
- # Weak Reference class that does not bother GCing.
2
- #
3
- # Usage:
4
- # foo = Object.new
5
- # foo = Object.new
6
- # p foo.to_s # original's class
7
- # foo = WeakRef.new(foo)
8
- # p foo.to_s # should be same class
9
- # ObjectSpace.garbage_collect
10
- # p foo.to_s # should raise exception (recycled)
11
-
12
- require "#{$knjpath}rhodes/delegate"
13
- require "thread"
14
-
15
- class WeakRef < Delegator
16
-
17
- class RefError < StandardError
18
- end
19
-
20
- @@id_map = {} # obj -> [ref,...]
21
- @@id_rev_map = {} # ref -> obj
22
- @@mutex = Mutex.new
23
- @@final = lambda {|id|
24
- @@mutex.synchronize {
25
- rids = @@id_map[id]
26
- if rids
27
- for rid in rids
28
- @@id_rev_map.delete(rid)
29
- end
30
- @@id_map.delete(id)
31
- end
32
- rid = @@id_rev_map[id]
33
- if rid
34
- @@id_rev_map.delete(id)
35
- @@id_map[rid].delete(id)
36
- @@id_map.delete(rid) if @@id_map[rid].empty?
37
- end
38
- }
39
- }
40
-
41
- def initialize(orig)
42
- @__id = orig.object_id
43
- ObjectSpace.define_finalizer orig, @@final
44
- ObjectSpace.define_finalizer self, @@final
45
- @@mutex.synchronize {
46
- @@id_map[@__id] = [] unless @@id_map[@__id]
47
- }
48
- @@id_map[@__id].push self.object_id
49
- @@id_rev_map[self.object_id] = @__id
50
- super
51
- end
52
-
53
- def __getobj__
54
- unless @@id_rev_map[self.object_id] == @__id
55
- Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2)
56
- end
57
- begin
58
- ObjectSpace._id2ref(@__id)
59
- rescue RangeError
60
- Kernel::raise RefError, "Invalid Reference - probably recycled", Kernel::caller(2)
61
- end
62
- end
63
- def __setobj__(obj)
64
- end
65
-
66
- def weakref_alive?
67
- @@id_rev_map[self.object_id] == @__id
68
- end
69
- end
70
-
71
- if __FILE__ == $0
72
- # require 'thread'
73
- foo = Object.new
74
- p foo.to_s # original's class
75
- foo = WeakRef.new(foo)
76
- p foo.to_s # should be same class
77
- ObjectSpace.garbage_collect
78
- ObjectSpace.garbage_collect
79
- p foo.to_s # should raise exception (recycled)
80
- end