Project

femto

0.0
No commit activity in last 3 years
No release in over 3 years
A tiny web framework
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0

Runtime

~> 1.10.0
~> 4.0.4
~> 1.10.0
~> 1.4.5
~> 2.0.1
 Project Readme

Femto

A tiny web framework.

Installation

$ gem install femto

If you're using bundler, 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

Fields can be validated before saving by using ModelCreator#validate:

model :user do |m|
    m.validate(:password) { |val| val.length > 8 }
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].

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

femto is the SI prefix for 10-15