Class: PrestoCore::App

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/presto/core/app.rb

Constant Summary

Constant Summary

Constants included from Utils

Utils::PATH_MODIFIERS, Utils::STATUS__NOT_FOUND, Utils::STATUS__OK, Utils::STATUS__PERMANENT_REDIRECT, Utils::STATUS__REDIRECT, Utils::STATUS__RESTRICTED, Utils::STATUS__SERVER_ERROR

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils

build_path, #extract_controllers, is_controller?, normalize_path, rootify_url

Constructor Details

- (App) initialize(&proc)

initializing new App

Examples:

app = Presto.new
app = Presto.new
app.helper SomeModule
app.use SomeMiddleware
app = Presto.new do
  helper SomeModule
  use SomeMiddleware
end


43
44
45
46
47
48
49
50
51
# File 'lib/presto/core/app.rb', line 43

def initialize &proc
  self.class.root
  @slices = []
  @middleware = []
  @helpers = {}
  @discover_controllers = true
  @sweep_sessions_at = ::Time.now.utc.to_i + ::Presto.setup.http.session.sweep_interval
  self.instance_exec(&proc) if proc
end

Instance Attribute Details

- (Object) middleware (readonly)

Returns the value of attribute middleware



28
29
30
# File 'lib/presto/core/app.rb', line 28

def middleware
  @middleware
end

- (Object) sweep_sessions_at

Returns the value of attribute sweep_sessions_at



29
30
31
# File 'lib/presto/core/app.rb', line 29

def sweep_sessions_at
  @sweep_sessions_at
end

Class Method Details

+ (Object) mount(*args, &proc)



11
12
13
# File 'lib/presto/core/app.rb', line 11

def mount *args, &proc
  self.new.mount *args, &proc
end

+ (Object) root



7
8
9
# File 'lib/presto/core/app.rb', line 7

def root
  @root ||= ::Dir.pwd
end

+ (Object) run(*args)



15
16
17
# File 'lib/presto/core/app.rb', line 15

def run *args
  self.new.run *args
end

+ (Object) started



19
20
21
# File 'lib/presto/core/app.rb', line 19

def started
  @started = true
end

+ (Boolean) started?

Returns:

  • (Boolean)


23
24
25
# File 'lib/presto/core/app.rb', line 23

def started?
  @started
end

Instance Method Details

- (Object) finalize

beside explicitly mounted controllers, Presto will also search for dynamically defined controllers. setting finalize to true will avoid this operation.



85
86
87
88
# File 'lib/presto/core/app.rb', line 85

def finalize
  @discover_controllers = false
  self
end

- (Object) helper(*helpers, &proc)

avoid redundant inclusion of same modules in multiple controllers.

Examples:

include Helper module into all App controllers

app = Presto.new
app.helper Helper1, Helper2
app.helper HelperN do
  # execute some code inside controller after it included the helper
end

Parameters:

  • helpers (Array)
  • proc (Proc)

    block to be executed inside controller after helper included



152
153
154
155
# File 'lib/presto/core/app.rb', line 152

def helper *helpers, &proc
  helpers.each { |h| @helpers[h] = proc }
  self
end

- (Object) map

current app map



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/presto/core/app.rb', line 91

def map

  if @discover_controllers

    controllers = ::ObjectSpace.each_object(::Class).
        select { |c| is_controller?(c) && !c.ctrl.mounted? }

    if controllers.size > 0
      @slices << ::PrestoCore::Slice.new
      @slices.last.add_controllers *controllers
    end
  end

  map = {}
  @slices.each do |slice|
    slice.controllers.select { |n| n.ctrl.map }.each do |controller|
      controller.ctrl.map.each_pair do |route, setup|
        map[route] = [controller, setup.map { |rm, im| '%s: #%s' % [rm, im] }.join(', ')]
      end
    end
  end

  if map.size == 0
    puts
    puts '*'*50
    puts ' ... No slices nor controllers mounted, exiting ...'
    puts '*'*50
    puts
    exit 1
  end

  def map.to_s
    controllers = []
    offset = self.sort { |a, b| b.first.size <=> a.first.size }.first.first.size + 5
    self.map do |s|
      route, controller, action = s.first, *s.last
      out = '%s  %s%s%s' % [
          ("\n%s\n" % controller unless controllers.include?(controller)),
          route,
          ' '*(offset-route.size),
          action
      ]
      controllers << controller
      out
    end.join("\n") << "\n\n"
  end

  map
end

- (Object) mount(namespace, *paths, &proc)

mounting a namespace into a given root and optionally configure mounted slice. controllers under given namespace will use here setup beside own internal setup.

Examples:

app = Presto.new
app.mount SomeNamespace do
  # locking all controllers
  http.auth {|u,p| [u,p] == ['admin', 'ssp']}
  # middleware to be used by all controllers
  http.use SomeMiddleware, 'with', 'some' => 'args'
  # defining view root for all controllers
  view.root '/some/path'
  # etc
end

Parameters:

  • namespace (Class, Module)

    containing an arbitrary number of Presto controllers.

  • *paths (String, Symbol)

    first one is treated as root, the rest ones are treated as canonicals

  • proc (Proc)

    used to config mounted slice



70
71
72
73
74
# File 'lib/presto/core/app.rb', line 70

def mount namespace, *paths, &proc
  slice = ::PrestoCore::Slice.new(namespace, *paths, &proc)
  @slices << slice
  self
end

- (Object) run(opts = {})

a handy method to start app without invoke Rack::Handler manually

by default, it will use WEBrick server and default WEBrick port. it accepts :server option and any option accepted by selected(or default) server:

Examples:

use Thin server with its default port

app.run server: :Thin

use EventedMongrel server with custom options

app.run server: :EventedMongrel, Port: 9090, num_processors: 1000

Parameters:

  • opts (Hash) (defaults to: {})


168
169
170
171
172
# File 'lib/presto/core/app.rb', line 168

def run opts = {}
  server = opts.delete(:server)
  server && ::Rack::Handler.const_defined?(server) || server = :WEBrick
  ::Rack::Handler.const_get(server).run builder, opts
end

- (Object) test_app



180
181
182
183
# File 'lib/presto/core/app.rb', line 180

def test_app
  finalize
  builder false
end

- (Object) to_app Also known as: app



174
175
176
# File 'lib/presto/core/app.rb', line 174

def to_app
  builder
end

- (Object) use(ware, *args, &proc)

define middleware to be used by app



77
78
79
80
# File 'lib/presto/core/app.rb', line 77

def use ware, *args, &proc
  @middleware << {ware: ware, args: args, block: proc}
  self
end