Module: RGame::Engine::Signal::DSL
Overview
Class-level DSL for declaring signal "slots" so a host class need not hand-write
the connect-forwarding boilerplate. extend Engine::Signal::DSL, then:
signal :on_clicked # a no-arg signal
signal :on_changed, ChangeSignal # a typed signal (Signal.define(:index, :value))
For :on_clicked this generates:
def on_clicked(&block) = on_clicked_signal.connect(&block) # public: subscribe, returns handle
def on_clicked_signal = (@on_clicked ||= type.new) # private: the Signal, to emit on
The Signal is built lazily on first use, so the host wires nothing in #initialize;
emit from inside the host via the private <name>_signal reader.
Instance Method Summary collapse
Instance Method Details
#signal(name, type = Signal.define) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rgame/engine/signal.rb', line 21 def signal(name, type = Signal.define) ivar = :"@#{name}" reader = :"#{name}_signal" define_method(reader) do instance_variable_get(ivar) || instance_variable_set(ivar, type.new) end private reader define_method(name) { |&block| send(reader).connect(&block) } end |