Class: Webmachine::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/webmachine/application.rb

Overview

How to get your Webmachine app running:

MyApp = Webmachine::Application.new do |app|
  app.routes do
    add [:*], AssetResource
  end

  app.configure do |config|
    config.port = 8888
  end
end

MyApp.run

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration = Configuration.default, dispatcher = Dispatcher.new) {|app| ... } ⇒ Application

Create an Application instance

An instance of application contains Adapter configuration and a Dispatcher instance which can be configured with Routes.

Parameters:

Yields:

  • (app)

    a block in which to configure this Application

Yield Parameters:

  • the (Application)

    Application instance being initialized



44
45
46
47
48
49
# File 'lib/webmachine/application.rb', line 44

def initialize(configuration = Configuration.default, dispatcher = Dispatcher.new)
  @configuration = configuration
  @dispatcher    = dispatcher

  yield self if block_given?
end

Instance Attribute Details

#configurationConfiguration

Returns the current configuration.

Returns:



27
28
29
# File 'lib/webmachine/application.rb', line 27

def configuration
  @configuration
end

#dispatcherDispatcher (readonly)

Returns the current dispatcher.

Returns:



30
31
32
# File 'lib/webmachine/application.rb', line 30

def dispatcher
  @dispatcher
end

Instance Method Details

#adapterObject

Returns an instance of the configured web-server adapter.

Returns:

  • an instance of the configured web-server adapter

See Also:



58
59
60
# File 'lib/webmachine/application.rb', line 58

def adapter
  @adapter ||= adapter_class.new(self)
end

#adapter_classObject

Returns an instance of the configured web-server adapter.

Returns:

  • an instance of the configured web-server adapter

See Also:



64
65
66
# File 'lib/webmachine/application.rb', line 64

def adapter_class
  Adapters.const_get(configuration.adapter)
end

#configure {|config| ... } ⇒ Application

Configure the web server adapter via the passed block

Returns the receiver so you can chain it with Application#run

Yields:

  • (config)

    a block in which to set configuration values

Yield Parameters:

Returns:



94
95
96
97
# File 'lib/webmachine/application.rb', line 94

def configure
  yield configuration if block_given?
  self
end

#routes(&block) ⇒ Application, Array<Route>

Evaluates the passed block in the context of Dispatcher for use in adding a number of routes at once.

Returns:

  • (Application, Array<Route>)

    self if configuring, or an Array of Routes otherwise

See Also:



75
76
77
78
79
80
81
82
# File 'lib/webmachine/application.rb', line 75

def routes(&block)
  if block_given?
    dispatcher.instance_eval(&block)
    self
  else
    dispatcher.routes
  end
end

#runObject

Starts this Application serving requests



52
53
54
# File 'lib/webmachine/application.rb', line 52

def run
  adapter.run
end