Sha256: 92d6add8c571846c251e6ad5b558a88574226045bc1fca7454c04807348caa99

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module Marko
  # Pluggable mixin serves for dependency injection
  #
  # @example
  #   class Storage
  #     extend Pluggable
  #     def call
  #     end
  #   end
  #
  #   StoragePort = Storage.port
  #
  #   class SequelStorage < Storage
  #   end
  #
  #   StoragePort.plugged = SequelStorage.new
  #
  #   require 'forwardable'
  #   class Service
  #     extend Forwardable
  #     def_delegator :StoragePort, :plugged, :storage
  #     def call
  #       storage.call
  #     end
  #   end
  module Pluggable

    def plug
      klass = self
      Module.new {
        extend Plug;
        plug klass
      }
    end

    module Plug
      def plug(klass)
        @klass = klass
      end

      def plugged
        fail "unknown @klass" unless @klass
        @plugged ||= @klass.new
      end

      def plugged=(obj)
        fail ArgumentError.new("required an instance of #{@klass}"
        ) unless obj.is_a?(@klass)
        @plugged = obj
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
marko-0.3.0 lib/marko/gadgets/pluggable.rb
marko-0.1.0 lib/marko/gadgets/pluggable.rb