Sha256: cd3f98e8e0ac9a363a7ab7500023ab06c48fef685eb329fd07896dca8c5bb3a8

Contents?: true

Size: 1.59 KB

Versions: 12

Compression:

Stored size: 1.59 KB

Contents

module Coprl
  module Presenters
    class Registry
      class << self
        include Coprl::Trace
      end

      @registry = {}

      def self.registry
        @registry
      end

      def self.define(name, namespace, options, &block)
        namespace = Array(namespace).map(&:to_s)
        fq_name = namespace.any? ? namespace.join(':') + ':' + name.to_s : name.to_s
        registry[fq_name] = Coprl::Presenters::DSL::Definition.new(name, namespace, options, &block)
      end

      def self.load(directory)
        files = File.join(directory, '**', '*.pom')
        Dir.glob(files) do |file|
          Kernel.load file
        end
      end

      def self.finalize
        registry.each do |key, definition|
          begin
            register(key, build(definition))
          rescue Exception => e
            logger.error {"Failed to load presenter #{key}: #{e.inspect}."}
            raise e
          end
        end
      end

      # This method empties out the dsl definitions
      # It should ONLY be used for testing purposes
      def self.reset!
        registry.clear
      end

      private

      def self.register(name, definition)
        return Coprl::Presenters::App.register(name: name, presenter: definition) unless Coprl::Presenters::App.registered?(name)
      end

      def self.build(definition)
        definition.build
      end
    end

    def self.define(name, namespace: nil, options: {}, &block)
      unless namespace
        namespace = name.to_s.split(':')
        name = namespace.pop
      end
      Registry.define(name, namespace, options, &block)
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
coprl-3.0.0.beta.12 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.11 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.10 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.9 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.8 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.7 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.6 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.5 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.4 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.3 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.2 lib/coprl/presenters/registry.rb
coprl-3.0.0.beta.1 lib/coprl/presenters/registry.rb