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.



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

def initialize(obj)
  __setobj__(obj)
end

Class Method Details

.const_missing(n) ⇒ Object

:stopdoc:



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

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

.delegating_block(mid) ⇒ Object

:nodoc:



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

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

.public_apiObject

:nodoc:



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

def self.public_api # :nodoc:
  @delegator_api
end

Instance Method Details

#!Object

Delegates ! to the __getobj__



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

def !
  !__getobj__
end

#!=(obj) ⇒ Object

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



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

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

#==(obj) ⇒ Object

Returns true if two objects are considered of equal value.



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

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.



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

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.



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

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

#eql?(obj) ⇒ Boolean

Returns true if two objects are considered of equal value.

Returns:



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

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.



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

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

#frozen?Boolean

Returns:



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

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

#marshal_dumpObject

Serialization support for the object returned by __getobj__.



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

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.



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

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.



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

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.



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

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.



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

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:



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

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