Class: Dommy::Bridge::Constructor
- Inherits:
-
Object
- Object
- Dommy::Bridge::Constructor
- 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
- #__js_call__(method, args) ⇒ Object
- #__js_new__(args) ⇒ Object
-
#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.
-
#initialize(&block) ⇒ Constructor
constructor
A new instance of Constructor.
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 |