首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Delegator

Parent:BasicObject

该库提供了三种不同的方法将方法调用委托给对象。最简单的就是SimpleDelegator。将一个对象传递给构造函数,该对象支持的所有方法都将被委派。该对象可以在之后更改。

更进一步,顶层的DelegateClass方法允许您通过类继承轻松设置委派。这是相当灵活的,因此可能是这个库最常见的用途。

最后,如果您需要完全控制委派方案,则可以从抽象类Delegator继承并根据需要进行自定义。(如果您发现自己需要这种控制,请查看标准库中的Forwardable,它可能更适合您的需求。)

SimpleDelegator的实现是使用Delegator的一个很好的例子:

代码语言:javascript
复制
class SimpleDelegator < Delegator
  def __getobj__
    @delegate_sd_obj # return object we are delegating to, required
  end

  def __setobj__(obj)
    @delegate_sd_obj = obj # change delegation object,
                           # a feature we're providing
  end
end

注意

请注意,RDoc不会检测委托方法。

公共类方法

new(obj) Show source

obj传递给委托方法调用。obj支持的所有方法都将被委托。

代码语言:javascript
复制
# File lib/delegate.rb, line 70
def initialize(obj)
  __setobj__(obj)
end

公共实例方法

!() Show source

Delegates ! to the _getobj_

代码语言:javascript
复制
# File lib/delegate.rb, line 150
def !
  !__getobj__
end

!=(obj) Show source

如果两个对象不被视为相同的值,则返回true。

代码语言:javascript
复制
# File lib/delegate.rb, line 142
def !=(obj)
  return false if obj.equal?(self)
  __getobj__ != obj
end

==(obj) Show source

如果两个对象被认为具有相同的值,则返回true。

代码语言:javascript
复制
# File lib/delegate.rb, line 134
def ==(obj)
  return true if obj.equal?(self)
  self.__getobj__ == obj
end

__getobj__() Show source

此方法必须由子类重写,并且应该返回正在委派给的对象方法调用。

代码语言:javascript
复制
# File lib/delegate.rb, line 158
def __getobj__
  __raise__ ::NotImplementedError, "need to define `__getobj__'"
end

__raise__()

Alias for: raise

__setobj__(obj) Show source

此方法必须由子类重写并将对象委托更改为obj

代码语言:javascript
复制
# File lib/delegate.rb, line 166
def __setobj__(obj)
  __raise__ ::NotImplementedError, "need to define `__setobj__'"
end

freeze() Show source

冻结由_ getobj _和self 返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 228
  

marshal_dump() Show source

序列化支持由_ getobj _ 返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 173
def marshal_dump
  ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
  [
    :__v2__,
    ivars, ivars.map {|var| instance_variable_get(var)},
    __getobj__
  ]
end

marshal_load(data) Show source

重新初始化序列化对象的委派。

代码语言:javascript
复制
# File lib/delegate.rb, line 185
def marshal_load(data)
  version, vars, values, obj = data
  if version == :__v2__
    vars.each_with_index {|var, i| instance_variable_set(var, values[i])}
    __setobj__(obj)
  else
    __setobj__(data)
  end
end

method_missing(m, *args, &block) Show source

通过_ getobj _ 处理代表团的效力。

调用超类方法BasicObject#method_missing

代码语言:javascript
复制
# File lib/delegate.rb, line 77
def method_missing(m, *args, &block)
  r = true
  target = self.__getobj__ {r = false}

  if r && target.respond_to?(m)
    target.__send__(m, *args, &block)
  elsif ::Kernel.respond_to?(m, true)
    ::Kernel.instance_method(m).bind(self).(*args, &block)
  else
    super(m, *args, &block)
  end
end

methods(all=true) Show source

以此对象的_getobj _方法的联合方式返回此委托对象可用的方法。

调用超类方法

代码语言:javascript
复制
# File lib/delegate.rb, line 109
def methods(all=true)
  __getobj__.methods(all) | super
end

protected_methods(all=true) Show source

返回此委托对象可用的方法,作为此对象和_ getobj _ protected方法的联合。

调用超类方法

代码语言:javascript
复制
# File lib/delegate.rb, line 125
def protected_methods(all=true)
  __getobj__.protected_methods(all) | super
end

public_methods(all=true) Show source

返回此委托对象可用的方法,作为此对象和_ getobj _ public方法的联合。

调用超类方法

代码语言:javascript
复制
# File lib/delegate.rb, line 117
def public_methods(all=true)
  __getobj__.public_methods(all) | super
end

raise() Show source

如果您的Delegator没有委托raise方法调用的对象,请使用__raise__。

代码语言:javascript
复制
# File lib/delegate.rb, line 66
  

Also aliased as: __raise__

respond_to_missing?(m, include_private) Show source

通过_ getobj _ 转发呼叫来检查代理对象提供的方法。

代码语言:javascript
复制
# File lib/delegate.rb, line 94
def respond_to_missing?(m, include_private)
  r = true
  target = self.__getobj__ {r = false}
  r &&= target.respond_to?(m, include_private)
  if r && include_private && !target.respond_to?(m, false)
    warn "#{caller(3)[0]}: delegator does not forward private method \##{m}"
    return false
  end
  r
end

taint() Show source

占用_ getobj _和自身返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 218
  

trust() Show source

信任由_ getobj _和自身返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 208
  

untaint() Show source

清除_ getobj _和self 返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 223
  

untrust() Show source

信任 _ getobj _和self自身返回的对象。

代码语言:javascript
复制
# File lib/delegate.rb, line 213
  

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com