Class: Delegator

Inherits:
BasicObject
Defined in:
opal/stdlib/delegate.rb

Direct Known Subclasses

SimpleDelegator

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Delegator

Pass in the obj to delegate method calls to. All methods supported by obj will be delegated to.



82
83
84
# File 'opal/stdlib/delegate.rb', line 82

def initialize(obj)
  __setobj__(obj)
end

Class Method Details

.const_missing(n) ⇒ Object

:stopdoc:



67
68
69
# File 'opal/stdlib/delegate.rb', line 67

def self.const_missing(n)
  ::Object.const_get(n)
end

.delegating_block(mid) ⇒ Object

:nodoc:



359
360
361
362
363
364
# File 'opal/stdlib/delegate.rb', line 359

def Delegator.delegating_block(mid) # :nodoc:
  ->(*args, &block) do
    target = __getobj__
    target.__send__(mid, *args, &block)
  end.ruby2_keywords
end

.public_apiObject

:nodoc:



256
257
258
# File 'opal/stdlib/delegate.rb', line 256

def self.public_api # :nodoc:
  @delegator_api
end

Instance Method Details

#!Object

Delegates ! to the __getobj__



187
188
189
# File 'opal/stdlib/delegate.rb', line 187

def !
  !__getobj__
end

#!=(obj) ⇒ Object

Returns true if two objects are not considered of equal value.



171
172
173
174
# File 'opal/stdlib/delegate.rb', line 171

def !=(obj)
  return false if obj.equal?(self)
  __getobj__ != obj
end

#==(obj) ⇒ Object

Returns true if two objects are considered of equal value.



163
164
165
166
# File 'opal/stdlib/delegate.rb', line 163

def ==(obj)
  return true if obj.equal?(self)
  __getobj__ == obj
end

#__getobj__Object

This method must be overridden by subclasses and should return the object method calls are being delegated to.



195
196
197
# File 'opal/stdlib/delegate.rb', line 195

def __getobj__
  __raise__ ::NotImplementedError, "need to define `__getobj__'"
end

#__setobj__(obj) ⇒ Object

This method must be overridden by subclasses and change the object delegate to obj.



203
204
205
# File 'opal/stdlib/delegate.rb', line 203

def __setobj__(obj)
  __raise__ ::NotImplementedError, "need to define `__setobj__'"
end

#eql?(obj) ⇒ Boolean

Returns true if two objects are considered of equal value.

Returns:



179
180
181
182
# File 'opal/stdlib/delegate.rb', line 179

def eql?(obj)
  return true if obj.equal?(self)
  obj.eql?(__getobj__)
end

#freezeObject

:method: freeze Freeze both the object returned by __getobj__ and self.



245
246
247
248
249
# File 'opal/stdlib/delegate.rb', line 245

def freeze
  __getobj__.freeze
  `$freeze_props(self)`
  `$freeze(self)`
end

#frozen?Boolean

Returns:



251
252
253
# File 'opal/stdlib/delegate.rb', line 251

def frozen?
  `(self.$$frozen || false)`
end

#marshal_dumpObject

Serialization support for the object returned by __getobj__.



210
211
212
213
214
215
216
217
# File 'opal/stdlib/delegate.rb', line 210

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) ⇒ Object

Reinitializes delegation from a serialized object.



222
223
224
225
226
227
228
229
230
# File 'opal/stdlib/delegate.rb', line 222

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

#methods(all = true) ⇒ Object

Returns the methods available to this delegate object as the union of this object's and __getobj__ methods.



138
139
140
# File 'opal/stdlib/delegate.rb', line 138

def methods(all = true)
  __getobj__.methods(all) | super
end

#protected_methods(all = true) ⇒ Object

Returns the methods available to this delegate object as the union of this object's and __getobj__ protected methods.



154
155
156
# File 'opal/stdlib/delegate.rb', line 154

def protected_methods(all = true)
  __getobj__.protected_methods(all) | super
end

#public_methods(all = true) ⇒ Object

Returns the methods available to this delegate object as the union of this object's and __getobj__ public methods.



146
147
148
# File 'opal/stdlib/delegate.rb', line 146

def public_methods(all = true)
  __getobj__.public_methods(all) | super
end

#respond_to_missing?(m, include_private) ⇒ Boolean

Checks for a method provided by this the delegate object by forwarding the call through __getobj__.

Returns:



106
107
108
109
110
111
112
113
114
115
# File 'opal/stdlib/delegate.rb', line 106

def respond_to_missing?(m, include_private)
  r = true
  target = __getobj__ { r = false }
  r &&= target_respond_to?(target, m, include_private)
  if r && include_private && !target_respond_to?(target, m, false)
    warn "delegator does not forward private method \##{m}", uplevel: 3
    return false
  end
  r
end