Class: Bruter::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/bruter/application.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Initializing a new Bruter::Application or a subclass of it will instantiate a new Router and Sprockets::Environment and subsequently add all routes to the router and asset paths to the asset environment.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/bruter/application.rb', line 119

def initialize
  @router = Router.new
  @assets = Sprockets::Environment.new
  #
  self.class.routes.each do |r|
    path      = r[:path]
    options   = r[:options]
    to        = options.delete(:to)
    redirect  = options.delete(:redirect)
    static    = options.delete(:static)
    route     = @router.add(path)
    if to
      route.to(to)
    elsif redirect
      route.redirect(redirect)
    elsif static
      route.static(self.class.absolute_path(static))
    end
    options.each { |k, v| route.send(k, v) if route.respond_to?(k) }
  end
  #
  self.class.asset_paths.each do |path|
    @assets.append_path(self.class.absolute_path(path))
  end
end

Class Attribute Details

._directoryObject

Returns the value of attribute _directory.



5
6
7
# File 'lib/bruter/application.rb', line 5

def _directory
  @_directory
end

.asset_pathsObject

Ensure that :asset_paths defaults to an array.



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

def asset_paths
  @asset_paths
end

.routesObject

Ensure that :routes defaults to an array.



21
22
23
# File 'lib/bruter/application.rb', line 21

def routes
  @routes
end

Instance Attribute Details

#assetsObject

Returns the value of attribute assets.



114
115
116
# File 'lib/bruter/application.rb', line 114

def assets
  @assets
end

#routerObject

Returns the value of attribute router.



114
115
116
# File 'lib/bruter/application.rb', line 114

def router
  @router
end

Class Method Details

.absolute_path(path) ⇒ Object

Retrieves the absolute path of the argument path relative to where Bruter::Application was subclassed (:_directory, see above).



16
17
18
# File 'lib/bruter/application.rb', line 16

def absolute_path(path)
  File.expand_path(path, _directory)
end

.add(path, options = {}) ⇒ Object

Adds a route to the application. Accepts the path of the route and a hash of options. The path is required and can include named variables (‘/post/:id’) and globs (‘/tags/*tags’).

The options hash must include either a :to, :redirect or :static key; the route will not be recognized if none is present. If more than one is present the preceding order of keys will serve as precedence.

The :to key should be either a Rack application or a string representing a Mustache view class. (eg ‘my_blog/index’ => MyBlog::Index)

The :redirect key should be a string representing the new path or url that the route should redirect to.

The :static key should be a string representing a path on the file system (relative to where the method was called).

Additional options: Add a name to the route (:name => :posts) Add a regex matching condition (:matching => => /d+/) Add a HTTP method constraint (:request_method => [‘POST’, ‘HEAD’])

Examples: add ‘/’, to: ‘views/posts/index’, name: :posts add ‘/posts/:id’, to: ‘views/posts/show’, name: :post add ‘/posts/:id’, to: ‘views/posts/update’, request_method: [‘PUT’] add ‘/tags/*tags’, to: ‘views/posts/tagged’, name: :tagged

add ‘/:i/is-an-integer’, to: ‘views/integer’, matching: /d+/

add ‘/old-location’, redirect: ‘/new-location’ add ‘/images/*’, static: ‘images’

For more information see the HttpRouter library (github.com/joshbuddy/http_router)



66
67
68
# File 'lib/bruter/application.rb', line 66

def add(path, options = {})
  routes << {path: path, options: options}
end

.delete(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘DELETE’.



86
87
88
# File 'lib/bruter/application.rb', line 86

def delete(path, options = {})
  add(path, options.merge(request_method: 'DELETE'))
end

.get(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘GET’.



71
72
73
# File 'lib/bruter/application.rb', line 71

def get(path, options = {})
  add(path, options.merge(request_method: 'GET'))
end

.head(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘HEAD’.



91
92
93
# File 'lib/bruter/application.rb', line 91

def head(path, options = {})
  add(path, options.merge(request_method: 'HEAD'))
end

.inherited(klass) ⇒ Object

Hack to set :_directory to the file location in which this class is subclassed. Those not subclassing Bruter::Mustache in a file may want to explicitly set :_directory.



10
11
12
# File 'lib/bruter/application.rb', line 10

def inherited(klass)
  klass._directory = File.dirname(caller.first[/^[^:]+/])
end

.options(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘OPTIONS’.



96
97
98
# File 'lib/bruter/application.rb', line 96

def options(path, options = {})
  add(path, options.merge(request_method: 'OPTIONS'))
end

.post(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘POST’.



76
77
78
# File 'lib/bruter/application.rb', line 76

def post(path, options = {})
  add(path, options.merge(request_method: 'POST'))
end

.put(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘PUT’.



81
82
83
# File 'lib/bruter/application.rb', line 81

def put(path, options = {})
  add(path, options.merge(request_method: 'PUT'))
end

.serve_assets(path) ⇒ Object

Tells the application’s asset environment to serve and compile assets in the path argument without passing to the router. The path is relative to where Bruter::Application is subclassed.



108
109
110
# File 'lib/bruter/application.rb', line 108

def serve_assets(path)
  asset_paths << path
end

.trace(path, options = {}) ⇒ Object

Convenience method setting the :request_method option to ‘TRACE’.



101
102
103
# File 'lib/bruter/application.rb', line 101

def trace(path, options = {})
  add(path, options.merge(request_method: 'TRACE'))
end

Instance Method Details

#call(env) ⇒ Object

A call to this application will first check to see if the path is in any of the asset environment’s paths. If so, the asset environment will attempt to serve and compile the asset; if not, action is passed to the router.



149
150
151
152
153
154
155
# File 'lib/bruter/application.rb', line 149

def call(env)
  if @assets.find_asset(env['PATH_INFO'].gsub(/\A\//, ''))
    @assets.call(env)
  else
    @router.call(env)
  end
end