Sha256: 00d152d34b7dc87fd387c983bbc48b3b74219600bc7aa4172f0d2932d78ec431

Contents?: true

Size: 982 Bytes

Versions: 3

Compression:

Stored size: 982 Bytes

Contents

require 'thread'
require 'lotus/router'

module Lotus
  class Container
    class Router < ::Lotus::Router
      def mount(app, options)
        app = app.new(path_prefix: options.fetch(:at)) if lotus_app?(app)
        super(app, options)
      end

      private

      def lotus_app?(app)
        app.ancestors.include? Lotus::Application
      end
    end

    attr_reader :routes

    def self.configure(options = {}, &blk)
      Mutex.new.synchronize do
        @@options       = options
        @@configuration = blk
      end
    end

    def initialize
      Mutex.new.synchronize do
        assert_configuration_presence!
        @routes = Router.new(&@@configuration)
      end
    end

    def call(env)
      @routes.call(env)
    end

    private
    def assert_configuration_presence!
      unless self.class.class_variable_defined?(:@@configuration)
        raise ArgumentError.new("#{ self.class } doesn't have any application mounted.")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lotusrb-0.5.0 lib/lotus/container.rb
lotusrb-0.4.1 lib/lotus/container.rb
lotusrb-0.4.0 lib/lotus/container.rb