Contributed Hobbit extensions.
Add this line to your application's Gemfile:
gem 'hobbit-contrib', require: 'hobbit/contrib'
# or this if you want to use master
# gem 'hobbit-contrib', github: 'patriciomacadden/hobbit-contrib', require: 'hobbit/contrib'
And then execute:
$ bundle
Or install it yourself as:
$ gem install hobbit-contrib
Each extension may have its own usage. In general, including the module will be enough.
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
# include hobbit session extension
include Hobbit::Session
# define your application
end
This module allows you to include images, javascripts and stylesheets in a easy way. To use this extension just include the module:
In config.ru
:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
# Put your assets in:
# * public/images
# * public/javascripts
# * public/stylesheets
use Rack::Static, root: 'public', urls: ['/images', '/javascripts', '/stylesheets']
include Hobbit::AssetTag
include Hobbit::EnhancedRender # see below
get '/' do
render 'index', {}, layout: 'layout'
end
end
run App.new
in views/layouts/layout.erb
:
<!DOCTYPE html>
<html>
<head>
<title>Hobbit::AssetTag</title>
<!--
becomes:
<script src="http://code.jquery.com/jquery-2.0.0.min.js" type="text/javascript"></script>
<script src="/javascripts/application.js" type="text/javascript"></script>
-->
<%= javascript 'http://code.jquery.com/jquery-2.0.0.min.js', 'application' %>
<!--
becomes:
<link href="/stylesheets/application.css" rel="stylesheet"/>
-->
<%= stylesheet 'application' %>
</head>
<body>
<%= yield %>
</body>
</html>
and in views/index.erb
:
<h1>Hobbit::AssetTag</h1>
<!-- becomes: /images/some-hobbit.png -->
<img src="<%= image_path 'some-hobbit.png' %>"/>
image_path
: Returns the path for a given image.javascript
: Returns a list of one or more script tags (see the example above).javascript_path
: Returns the path for a given javascript file. If you pass an url, it returns the given url. If you pass an arbitrary string, it returns/javascripts/#{url}.js
.stylesheet
: Returns a list of one or more link tags (see the example above).stylesheet_path
: Returns the path for a given stylesheet file. If you pass an url, it returns the given url. If you pass an arbitrary string, it returns/stylesheets/#{url}.css
.
This module extends the functionality of Hobbit::Render
. To use this extension
just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::EnhancedRender
get '/' do
# index will become views/index.erb
# use a layout (named 'layout'. It will become views/layouts/layout.erb)
render 'index', {}, layout: 'layout'
end
end
template_engine
: Returns the default template engine being used for this application. By default iserb
.layout_path
: Returns the layout path according to the given template. For instance,layout_path('layout')
will becomeviews/layouts/layout.erb
view_path
: Returns the view path according to the given template. For instance,view_path('index')
will becomeviews/index.erb
render
: It's the same as inHobbit::Render
, but you can render templates with a layout (See the example above).partial
: It's the same asrender
, but expands the template name to the views path, except that the template name will start with an underscore. For instance, if you callpartial 'partial'
, the path will becomeviews/_partial.erb
This extension allows you to control the application environment by using the provided methods. To use this extension just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::Environment
get '/' do
"currently in #{environment}"
end
end
run App.new
environment
: Returns the current environment. By default isENV['RACK_ENV']
.environment=()
: Sets the environment.development?
: Returns true if the current environment is:development
.production?
: Returns true if the current environment is:production
.test?
: Returns true if the current environment is:test
.
This extension provides a way of handling errors raised by your application. To use this extension just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::ErrorHandling
error Exception do |exception|
exception.message
end
get '/' do
raise Exception, 'Oops'
end
end
run App.new
errors
: Returns a hash with the exceptions being handled and its corresponding handler.error
: Sets a handler for a given exception.
Note: If you define more than one handler per exception the last one defined will have precedence over the others.
This extension provides a way of calling blocks before and after the evaluation of a route (just like sinatra's filters). To use this extension just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::Filter
def authenticate_user!
# ...
end
before do
authenticate_user!
end
get '/' do
# ...
end
end
run App.new
after
: Sets an after filter. Optionally, you can specify a route.before
: Sets a before filter. Optionally, you can specify a route.
Note: It is recommended to include Hobbit::Filter
before
Hobbit::ErrorHandling
if you want to use both extensions.
This module provides rendering to your hobbit application. To use this extension just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::Render
get '/' do
render 'views/index.erb'
end
get '/with-layout' do
render 'views/layout.erb' do
render 'views/index.erb'
end
end
end
render
: Renders the given template using tilt.
This module provides helper methods for handling user sessions. To use this extension just include the module:
require 'hobbit'
require 'hobbit/contrib'
class App < Hobbit::Base
include Hobbit::Session
post '/' do
session[:name] = 'hobbit'
end
get '/' do
"Hello #{session[:name]}!"
end
end
session
: Returns the user's session.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
See the LICENSE.