Sha256: dd0037601261df915c257820cf3fd50ede091490bd471dbc145c2ce445138721

Contents?: true

Size: 1021 Bytes

Versions: 3

Compression:

Stored size: 1021 Bytes

Contents

module Observed
  # Indicates that the class is pluggable (or extensible or a extension point).
  # "pluggable" means that the class included this module will be the outlet in where Observed plug-ins are plugged.
  #
  # @example
  # class Reader
  #   include Pluggable
  # end
  # class FooReader < Reader; end
  # class BarReader < Reader; end
  # Reader.plugins #=> [FooReader, BarReader]
  module Pluggable

    module ClassMethods
      def plugins
        @plugins ||= []
      end

      def inherited(klass)
        super if defined? super
        plugins << klass
      end

      def plugin_name(plugin_name=nil)
        @plugin_name = plugin_name if plugin_name
        @plugin_name
      end

      def find_plugin_named(plugin_name)
        plugins.find { |plugin| plugin.plugin_name == plugin_name }
      end

      def select_named_plugins
        plugins.select(&:plugin_name)
      end
    end

    class << self
      def included(klass)
        klass.extend ClassMethods
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
observed-0.2.0 lib/observed/pluggable.rb
observed-0.2.0.rc2 lib/observed/pluggable.rb
observed-0.2.0.rc1 lib/observed/pluggable.rb