Sha256: c663645bc23c2a3924c9df9791ee55bcf3592a34fa88cde11e2e7da012595df4

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module KO
  module Signals
    module InstanceMethods
      # TODO: write me!
      def respond_to_missing?(name, include_private = false); end

      def method_missing(name, *args, **params, &)
        sigs = signals.each.with_object({}) { |(_k, v), acc| acc[v.receiver_name] = v }
        super unless sigs.include?(name)

        define_singleton_method(name, &)

        sigs[name].connect(self)
      end

      private

      def signals = @signals ||= self.class.signals.transform_values(&:dup)

      def emit(name, *args) = send(name).call(*args)
    end

    module AddSignal
      def signal(name, *arg_types)
        signals[name.to_sym] = Signal.new(name, arg_types)

        d = respond_to?(:define_method) ? :define_method : :define_singleton_method
        send(d, name) { signals[name.to_sym] }
      end
    end

    class << self
      def extended(base)
        base.include(InstanceMethods)
        base.extend(AddSignal)
        base.include(AddSignal)
      end
    end

    # def method_added(name)
    #   pp([self, name])
    # end

    def inherited(child)
      super(child)
      child.signals.merge!(signals)
    end

    def signals = @signals ||= {}
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ko-0.1.2 lib/ko/signals.rb