Sha256: 454741f1e9e725fd75a01fac3f4e5db9935d69a796da7230df1abeda4de87c91

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 KB

Contents

require "angelo"

module Angelo
  class Base
    @angelo_main = true

    # Takes a block and/or modules that define methods that can be
    # used from within request handlers.  Methods can also be defined
    # at the top level but they get defined as Object instance methods
    # which isn't good.

    module DSL
      def helpers(*args, &block)
        args.each do |mod|
          include(mod)
        end
        block && class_exec(&block)
      end
    end

  end
end

# The intent here is to add the DSL to the top-level object "main" by
# creating an anonymous Angelo::Base subclass and forwarding the DSL
# methods to it from the top-level object.  Then run the app at exit.
#
# If angelo/main is required from somewhere other than the top level,
# all bets are off.

if self.to_s == "main"
  # We are probably at the top level.

  require "forwardable"
  self.extend Forwardable
  @angelo_app = Class.new(Angelo::Base)
  self.def_delegators :@angelo_app, *Angelo::Base::DSL.instance_methods

  at_exit do
    # Don't run @angelo_app on uncaught exceptions including exit
    # being called which raises SystemExit.  The rationale being that
    # exit means exit, not "run the server".
    if $!.nil?
      @angelo_app.run!
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
angelo-0.5.0 lib/angelo/main.rb
angelo-0.4.1 lib/angelo/main.rb
angelo-0.4.0 lib/angelo/main.rb