Sha256: dae9112d1087d6407c42fa694dae1c58a30227ed63f1a197390b561890d2e36c

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

module Voom
  module Presenters
    class Registry
      class << self
        include Voom::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] = Voom::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 Voom::Presenters::App.register(name: name, presenter: definition) unless Voom::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

6 entries across 6 versions & 1 rubygems

Version Path
voom-presenters-2.1.2 lib/voom/presenters/registry.rb
voom-presenters-2.1.0 lib/voom/presenters/registry.rb
voom-presenters-2.0.3 lib/voom/presenters/registry.rb
voom-presenters-2.0.2 lib/voom/presenters/registry.rb
voom-presenters-2.0.1 lib/voom/presenters/registry.rb
voom-presenters-2.0.0 lib/voom/presenters/registry.rb