Class: Deas::Server::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/server.rb

Constant Summary collapse

DEFAULT_ENV =
'development'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/deas/server.rb', line 144

def initialize
  @env                    = DEFAULT_ENV
  @root                   = ENV['PWD']
  @method_override        = true
  @show_exceptions        = false
  @verbose_logging        = true
  @middlewares            = []
  @init_procs             = []
  @error_procs            = []
  @before_route_run_procs = []
  @after_route_run_procs  = []
  @template_source        = nil
  @logger                 = Deas::NullLogger.new
  @router                 = Deas::Router.new

  @valid = nil
end

Instance Attribute Details

#after_route_run_procsObject

Returns the value of attribute after_route_run_procs.



141
142
143
# File 'lib/deas/server.rb', line 141

def after_route_run_procs
  @after_route_run_procs
end

#before_route_run_procsObject

Returns the value of attribute before_route_run_procs.



141
142
143
# File 'lib/deas/server.rb', line 141

def before_route_run_procs
  @before_route_run_procs
end

#envObject

Returns the value of attribute env.



138
139
140
# File 'lib/deas/server.rb', line 138

def env
  @env
end

#error_procsObject

Returns the value of attribute error_procs.



140
141
142
# File 'lib/deas/server.rb', line 140

def error_procs
  @error_procs
end

#init_procsObject

Returns the value of attribute init_procs.



140
141
142
# File 'lib/deas/server.rb', line 140

def init_procs
  @init_procs
end

#loggerObject

Returns the value of attribute logger.



142
143
144
# File 'lib/deas/server.rb', line 142

def logger
  @logger
end

#method_overrideObject

Returns the value of attribute method_override.



139
140
141
# File 'lib/deas/server.rb', line 139

def method_override
  @method_override
end

#middlewaresObject

Returns the value of attribute middlewares.



140
141
142
# File 'lib/deas/server.rb', line 140

def middlewares
  @middlewares
end

#rootObject

Returns the value of attribute root.



138
139
140
# File 'lib/deas/server.rb', line 138

def root
  @root
end

#routerObject

Returns the value of attribute router.



142
143
144
# File 'lib/deas/server.rb', line 142

def router
  @router
end

#show_exceptionsObject

Returns the value of attribute show_exceptions.



139
140
141
# File 'lib/deas/server.rb', line 139

def show_exceptions
  @show_exceptions
end

#template_sourceObject

Returns the value of attribute template_source.



142
143
144
# File 'lib/deas/server.rb', line 142

def template_source
  @template_source
end

#verbose_loggingObject

Returns the value of attribute verbose_logging.



139
140
141
# File 'lib/deas/server.rb', line 139

def verbose_logging
  @verbose_logging
end

Instance Method Details

#routesObject



170
171
172
# File 'lib/deas/server.rb', line 170

def routes
  self.router.routes
end

#urlsObject



166
167
168
# File 'lib/deas/server.rb', line 166

def urls
  self.router.urls
end

#valid?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/deas/server.rb', line 174

def valid?
  !!@valid
end

#validate!Object

for the config to be considered “valid”, a few things need to happen. The key here is that this only needs to be done once for each config.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/deas/server.rb', line 181

def validate!
  return @valid if !@valid.nil?  # only need to run this once per config

  # ensure all user and plugin configs are applied
  self.init_procs.each(&:call)
  raise Deas::ServerRootError if self.root.nil?

  # validate the router
  self.router.validate!

  # TODO: build final middleware stack when building the rack app, not here
  # (once Sinatra is removed)

  # prepend the method override middleware first.  This ensures that the
  # it is run before any other middleware
  self.middlewares.unshift([Rack::MethodOverride]) if self.method_override

  # append the show exceptions and logging middlewares next-to-last. This
  # ensures the logging and exception showing happens just before the
  # optional trailing slashes handling.  It should be just before the app
  # gets the request and just after the app sends a response (except for
  # trailing slashes - this should happen inside of the show exceptions
  # and logging behavior).
  self.middlewares << [Deas::ShowExceptions] if self.show_exceptions
  self.middlewares << [
    Deas::Logging.middleware_type(self.verbose_logging),
    self.logger
  ]

  # optionally add the trailing slashes middleware last b/c it should
  # happen inside of show exceptions and logging.  we want the behavior
  # to feel like app behavior to the rest of the middleware stack so it
  # needs to be just before the app gest the request and just after the
  # app sends a response.
  if self.router.trailing_slashes_set?
    self.middlewares << [Deas::TrailingSlashes, self.router]
  end

  self.middlewares.freeze

  @valid = true # if it made it this far, its valid!
end