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.



156
157
158
159
# File 'lib/dommy/bridge.rb', line 156

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



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

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`).



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

def __js_class_method_names__
  @class_methods.keys
end

#__js_new__(args) ⇒ Object



161
162
163
# File 'lib/dommy/bridge.rb', line 161

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.



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

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