Class: Dommy::Bridge::Constructor

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/bridge.rb

Overview

Block-as-constructor adapter — invoking __js_new__(args) calls the wrapped block with args and returns whatever the block produces. Used by Window to wire up new Event(init), new CustomEvent(init), etc. without hand-rolling a class for each constructor.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Constructor

Returns a new instance of Constructor.



167
168
169
170
# File 'lib/dommy/bridge.rb', line 167

def initialize(&block)
  @block = block
  @class_methods = {}
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



184
185
186
187
# File 'lib/dommy/bridge.rb', line 184

def __js_call__(method, args)
  handler = @class_methods[method.to_s]
  handler&.call(args)
end

#__js_class_method_names__Object

Names of the registered class-level (static) methods, so a JS host can expose them on the constructor function (e.g. URL.createObjectURL).



191
192
193
# File 'lib/dommy/bridge.rb', line 191

def __js_class_method_names__
  @class_methods.keys
end

#__js_new__(args) ⇒ Object



172
173
174
# File 'lib/dommy/bridge.rb', line 172

def __js_new__(args)
  @block.call(args)
end

#define_class_method(name, &block) ⇒ Object

Register a class-level method (e.g. URL.createObjectURL) that JS bridges resolve via __js_call__ on the constructor itself. Returns self for chaining.



179
180
181
182
# File 'lib/dommy/bridge.rb', line 179

def define_class_method(name, &block)
  @class_methods[name.to_s] = block
  self
end