Sinatra Decorator
sinatra-decorator is a gem for Sinatra.
Adds an object-oriented layer of presentation logic to your Sinatra application.
Installation
Add the following to your Gemfile
:
gem 'sinatra/decorator'
And then execute:
$ bundle
Examples
require 'sinatra'
require 'slim'
require 'sinatra/decorator'
require_relative 'models/post'
require_relative 'decorators/post_decorator'
get '/' do
@post = Post.new.decorate
slim :show
end
class Post
include Sinatra::Decorator::Decoratable
attr_accessor :id, :body
def initialize(params = {})
@id = params[:id] || 1
@body = params[:body] || "body"
end
end
class PostDecorator < Sinatra::Decorator::Base
def formated_body
object.body.gsub('b', 'a')
end
end
h1 = @post.id
div
= @post.formated_body
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
Copyright
- Copyright (c) 2013 Takeshi Yabe (the author of padrino-decorator).
- Copyright (c) 2014 Naotoshi Seo. See LICENSE for details.
Special Thanks
This gem was made on the basis of the padrino-decorator.
I greatly appreciate for the orignal author Mr. @tyabe.