Femto
A tiny web framework.
Installation
Add this to your Gemfile:
gem 'femto', github: 'augustt198/femto'
Usage
First, setup your views/templates folder:
# For example
@templates_dir = File.join __dir__, 'views'
Handle requests by passing a path and block to a HTTP verb method.
get '/' do
render 'home'
end
# The 'view' option automatically renders a template!
get '/about', view: 'about_page' do
# You can create variables to use in your views
@copyright_date = '2014'
end
The render method tries to render a view if a String is passed, but other content types can be rendered:
get '/status' do
render json: {system_status: 'good'}
end
Models
To use models, first you need specify the model adapter. MongoAdapter
is the only model adapter currently:
# Connect to database
Femto::Model::MongoAdapter.connect host: 'localhost', port: 27107, db: 'test'
Femto::Model.adapter = Femto::Model::MongoAdapter
Add models with the model
method, for example:
model :user do |m|
m.field :username
m.field :password
m.field :created_at
end
This will create the class User
. Use the model class to find, update, and insert:
user = User.new(username: 'foo', password: 'bar')
user.save # Saves to database or updates if it already exists
User.find # => [#<User:0x007fcb5bef8840>]
The parameter passed to the block in the model
method is the ModelCreator
.
You can add your own methods to the model:
model :user do |m|
m.set_method('confirmed?') { false }
end
You can also change the storage name of the model. The default is the model name + "s".
model :category do |m|
m.storage :categories
end
Layouts
You can define application-wide views by using the layout method.
layout view: 'layout' do
# This block would be called every page load
end
An example layout view:
<div class="container">
<!-- the yield keyword renders the current view -->
<%= yield %>
</div>
Custom Renderers
Femto comes with the renderers :json
, :pretty_json
, and :text
, but you can easily
add custom renderers.
For instance:
Femto::Renderer.renderer :custom do |content|
['text/plain', content.to_s.reverse]
end
All renderers should return an array with the Content-Type
at position [0]
, and the actual content
at position [1]
.
App Example
Contributing
- 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
femto is the SI prefix for 10-15