Sha256: e5b80fc907bd57c65692ee9eee6a7c3701fdfdf3367e51e27770350f4f1ff79e

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

require 'rack/server'
require 'rack/builder'

module Circuit
  module Rack
    # Extensions to Rack::Builder
    module BuilderExt
      # @return [Boolean] true if a default app is set
      def app?
        !!@run
      end

      # Duplicates the `@use` Array and `@map` Hash instance variables
      def initialize_copy(other)
        @use = other.instance_variable_get(:@use).dup
        unless other.instance_variable_get(:@map).nil?
          @map = other.instance_variable_get(:@map).dup
        end
      end
    end

    # A Rack::Builder variation that does not require a fully-compliant rackup
    # file; specifically that a default app (`run` directive) is not required.
    class Builder < ::Rack::Builder
      include BuilderExt

      # Parses the rackup (or circuit-rackup .cru) file.
      # @return [Circuit::Rack::Builder] the builder
      def self.parse_file(config, opts = ::Rack::Server::Options.new)
        # allow for objects that are String-like but don't respond to =~ 
        # (e.g. Pathname)
        config = config.to_s

        if config.to_s =~ /\.cru$/
          options = {}
          cfgfile = ::File.read(config)
          cfgfile.sub!(/^__END__\n.*\Z/m, '')
          builder = eval "%s.new {\n%s\n}"%[self, cfgfile], TOPLEVEL_BINDING, config
          return builder, options
        else
          # this should be a fully-compliant rackup file (or a constant name),
          # so use the real Rack::Builder, but return a Builder object instead 
          # of the app
          app, options = ::Rack::Builder.parse_file(config, opts)
          return self.new(app), options
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
circuit-0.2.0 lib/circuit/rack/builder.rb