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.



65
66
67
68
# File 'lib/dommy/bridge.rb', line 65

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

Instance Method Details

#__js_call__(method, args) ⇒ Object



82
83
84
85
# File 'lib/dommy/bridge.rb', line 82

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

#__js_new__(args) ⇒ Object



70
71
72
# File 'lib/dommy/bridge.rb', line 70

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.



77
78
79
80
# File 'lib/dommy/bridge.rb', line 77

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