Sha256: 1cf92fde7def8a94c756477698cfc645b272e25db5c905833a5b1466b25439a3

Contents?: true

Size: 836 Bytes

Versions: 3

Compression:

Stored size: 836 Bytes

Contents

module Generative
  class InvalidGenerator < StandardError; end
  class UnregisteredGenerator < StandardError; end

  class GeneratorManager

    attr_reader :generators

    def initialize(preregistered_generators={})
      @generators = Hash.new(preregistered_generators)
    end

    def register_generator(name, factory=nil, &generator)
      registerable = block_given? ? generator : factory
      msg = "#{registerable} must respond to :call"
      raise InvalidGenerator, msg unless generator_valid?(registerable)
      @generators.merge!(name => registerable)
    end

    def find_and_call(name, *args)
      generators.fetch(name) do
        raise UnregisteredGenerator, "#{name} generator not registered"
      end.call(*args)
    end

    def generator_valid?(generator)
      generator.respond_to?(:call)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
generative-0.2.5 lib/generative/generator_manager.rb
generative-0.2.4 lib/generative/generator_manager.rb
generative-0.2.3 lib/generative/generator_manager.rb