delegate 0.5.0 → 0.6.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/delegate.rb +28 -16
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4c3016b29b2e5293f2a1640abbf72cf0fe819f0745ca5bdf007c87ef7ed4abfe
4
- data.tar.gz: e3b42b7d7213d92697d0246250dee6ba6c7ac2e76455dabe68e991e4c126a51b
3
+ metadata.gz: 9fd09bc1c57e6112ca448780e877b5a07681c642937cf2457895be0dd05f990a
4
+ data.tar.gz: c9d806a0105bdd3a7ea904d8c829687bec4bf57fba9056ee8397186b4f05f108
5
5
  SHA512:
6
- metadata.gz: f9ffc701e9a7323a98cb5437abc35150507c1908232d8ef86bd0bd2c0aa3fa69da3ee9105d064427cda5d3e6bf073babf996c99e1783b494b01a729793228cad
7
- data.tar.gz: b906eb55858d81abc66dfce6c3468417cbac4023960ab143fca5e6c36a0fe00976a6d2bee3f55f07d988e5c9897a5037c6690ca91c7cb2fb536098fc64eaff1a
6
+ metadata.gz: aff48e6ba7051a1d421248d0a67cd760d3c680d5df7cb5778636d5d843a4afc3a96b10469872acfa51a702eb21c9f65df41cd205878975089dfecb0aa7c42853
7
+ data.tar.gz: 3482081f54f59d708691a45065f80f676d77571fc986f5f92c823c24fea6281f9b373f2a00095522fe0afc57803084864a647e06546791996affbdcb1c2f17d0
data/lib/delegate.rb CHANGED
@@ -40,7 +40,7 @@
40
40
  #
41
41
  class Delegator < BasicObject
42
42
  # The version string
43
- VERSION = "0.5.0"
43
+ VERSION = "0.6.0"
44
44
 
45
45
  kernel = ::Kernel.dup
46
46
  kernel.class_eval do
@@ -344,17 +344,15 @@ class SimpleDelegator < Delegator
344
344
  end
345
345
  end
346
346
 
347
- def Delegator.delegating_block(mid) # :nodoc:
348
- prok = lambda do |*args, &block|
347
+ def Delegator.delegating_code(mid) # :nodoc:
348
+ line = __LINE__+2
349
+ src = <<~RUBY
350
+ ruby2_keywords def #{mid}(*args, &block)
349
351
  target = self.__getobj__
350
- target.__send__(mid, *args, &block)
351
- end
352
- prok.ruby2_keywords
353
- if defined?(Ractor.shareable_proc)
354
- Ractor.shareable_proc(&prok)
355
- else
356
- prok
352
+ target.__send__(:'#{mid}', *args, &block)
357
353
  end
354
+ RUBY
355
+ [src, __FILE__, line]
358
356
  end
359
357
 
360
358
  #
@@ -405,6 +403,17 @@ def DelegateClass(superclass, &block)
405
403
  protected_instance_methods -= ignores
406
404
  public_instance_methods = superclass.public_instance_methods
407
405
  public_instance_methods -= ignores
406
+
407
+ normal, special = public_instance_methods.partition { |m| m.match?(/\A[a-zA-Z]\w*[!\?]?\z/) }
408
+
409
+ source = normal.map do |method|
410
+ "def #{method}(...); __getobj__.#{method}(...); end"
411
+ end
412
+
413
+ protected_instance_methods.each do |method|
414
+ source << "def #{method}(...); __getobj__.__send__(#{method.inspect}, ...); end"
415
+ end
416
+
408
417
  klass.module_eval do
409
418
  def __getobj__ # :nodoc:
410
419
  unless defined?(@delegate_dc_obj)
@@ -413,18 +422,21 @@ def DelegateClass(superclass, &block)
413
422
  end
414
423
  @delegate_dc_obj
415
424
  end
425
+
416
426
  def __setobj__(obj) # :nodoc:
417
427
  __raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
418
428
  @delegate_dc_obj = obj
419
429
  end
420
- protected_instance_methods.each do |method|
421
- define_method(method, Delegator.delegating_block(method))
422
- protected method
423
- end
424
- public_instance_methods.each do |method|
425
- define_method(method, Delegator.delegating_block(method))
430
+
431
+ class_eval(source.join(";"), __FILE__, __LINE__)
432
+
433
+ special.each do |method|
434
+ module_eval(*Delegator.delegating_code(method))
426
435
  end
436
+
437
+ protected(*protected_instance_methods)
427
438
  end
439
+
428
440
  klass.define_singleton_method :public_instance_methods do |all=true|
429
441
  super(all) | superclass.public_instance_methods
430
442
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delegate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto