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



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

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:



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

def configuration
  @configuration
end

#dispatcherDispatcher (readonly)

Returns the current dispatcher.

Returns:



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

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:



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

def adapter
  @adapter ||= adapter_class.new(configuration, dispatcher)
end

#adapter_classObject

Returns an instance of the configured web-server adapter.

Returns:

  • an instance of the configured web-server adapter

See Also:



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

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:



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

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:



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

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

#runObject

Starts this Application serving requests



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

def run
  adapter.run
end