Class: Yahns::Rack

Inherits:
Object
  • Object
show all
Defined in:
lib/yahns/rack.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ru, opts = {}) ⇒ Rack

Returns a new instance of Rack.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/yahns/rack.rb', line 18

def initialize(ru, opts = {})
  # always called after config file parsing, may be called after forking
  @app = lambda do
    if ru.respond_to?(:call)
      inner_app = ru.respond_to?(:to_app) ? ru.to_app : ru
    else
      inner_app = case ru
      when /\.ru$/
        raw = File.read(ru)
        raw.sub!(/^__END__\n.*/, '')
        eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
      else
        require ru
        Object.const_get(File.basename(ru, '.rb').capitalize)
      end
    end
    inner_app
  end
  @ru = ru
  @preload = opts[:preload]
  build_app! if @preload
end

Instance Attribute Details

#preloadObject (readonly)

Returns the value of attribute preload.



6
7
8
# File 'lib/yahns/rack.rb', line 6

def preload
  @preload
end

Class Method Details

.instance_key(*args) ⇒ Object

enforce a single instance for the identical config.ru



9
10
11
12
13
14
15
16
# File 'lib/yahns/rack.rb', line 9

def self.instance_key(*args)
  ru = args[0]

  # it's safe to expand_path now since we enforce working_directory in the
  # top-level config is called before any apps are created
  # ru may also be a Rack::Builder object or any already-built Rack app
  ru.respond_to?(:call) ? ru.object_id : File.expand_path(ru)
end

Instance Method Details

#app_after_forkObject



72
73
74
75
# File 'lib/yahns/rack.rb', line 72

def app_after_fork
  build_app! unless @preload
  @app
end

#app_defaultsObject

allow different HttpContext instances to have different Rack defaults



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/yahns/rack.rb', line 56

def app_defaults
  {
    # logger is set in http_context
    "rack.errors" => $stderr,
    "rack.multiprocess" => true,
    "rack.multithread" => true,
    "rack.run_once" => false,
    "rack.hijack?" => true,
    "rack.version" => [ 1, 2 ],
    "SCRIPT_NAME" => "",

    # this is not in the Rack spec, but some apps may rely on it
    "SERVER_SOFTWARE" => "yahns"
  }
end

#build_app!Object



48
49
50
51
52
53
# File 'lib/yahns/rack.rb', line 48

def build_app!
  if @app.respond_to?(:arity) && @app.arity == 0
    Gem.refresh if defined?(Gem) && Gem.respond_to?(:refresh)
    @app = @app.call
  end
end

#config_contextObject



41
42
43
44
45
46
# File 'lib/yahns/rack.rb', line 41

def config_context
  ctx_class = Class.new(Yahns::HttpClient)
  ctx_class.extend(Yahns::HttpContext)
  ctx_class.http_ctx_init(self)
  ctx_class
end