Sha256: 6d7ab7d72671810720e148cc7839a488348b936a189ae7e2bec38fdf8fc27ff9

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

module Alf
  module Support
    module Registry

      def listeners
        @listeners ||= []
      end

      def listen(&listener)
        listeners << listener
        registered.each do |what|
          method_name, factory = Registry.decode_registered(what)
          listener.call(method_name, factory)
        end
      end

      def registered
        @registered ||= []
      end
      alias :all :registered

      def each(&bl)
        registered.each(&bl)
      end

      def register(what, registry)
        method_name, factory = Registry.decode_registered(what)
        (class << registry; self; end).
          send(:define_method, method_name) do |*args, &block|
            factory.new(*args, &block)
          end
        registry.module_eval do
          registered << what
          listeners.each{|l| l.call(method_name, factory) }
        end
      end

    private

      def self.decode_registered(what)
        method_name, factory = nil, nil
        Array(what).each do |arg|
          method_name ||= arg if arg.is_a?(Symbol)
          factory     ||= arg if arg.is_a?(Class)
        end
        method_name = Support.rubycase_name(factory) unless method_name
        [method_name, factory]
      end

    end # module Registry
  end # module Support
end # module Alf

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/support/registry.rb
alf-core-0.16.2 lib/alf/support/registry.rb
alf-core-0.16.1 lib/alf/support/registry.rb
alf-core-0.16.0 lib/alf/support/registry.rb
alf-core-0.15.0 lib/alf/support/registry.rb
alf-core-0.14.0 lib/alf-support/alf/support/registry.rb
alf-core-0.13.1 lib/alf-support/alf/support/registry.rb
alf-core-0.13.0 lib/alf-support/alf/support/registry.rb