All Projects → fnando → burgundy

fnando / burgundy

Licence: MIT license
A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 150 lines.

Programming Languages

ruby
36898 projects - #4 most used programming language
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
CSS
56736 projects

Projects that are alternatives of or similar to burgundy

presenter
Easy class decoration.
Stars: ✭ 13 (-72.92%)
Mutual labels:  decorator, presenter
pyplugs
Decorator based plugin architecture for Python
Stars: ✭ 56 (+16.67%)
Mutual labels:  decorator
check-all
Multiple checkbox selection helper.
Stars: ✭ 70 (+45.83%)
Mutual labels:  decorator
vue-corator
this is vue decorator utils
Stars: ✭ 33 (-31.25%)
Mutual labels:  decorator
min
A decorator web framework for deno
Stars: ✭ 21 (-56.25%)
Mutual labels:  decorator
laravel-decorator
Easily decorate your method calls with laravel-decorator package
Stars: ✭ 125 (+160.42%)
Mutual labels:  decorator
singleton decorator
A testable singleton decorator
Stars: ✭ 39 (-18.75%)
Mutual labels:  decorator
ArtShredder
📱Banksy Shredder for iOS
Stars: ✭ 41 (-14.58%)
Mutual labels:  presenter
strongtyping
Decorator which checks whether the function is called with the correct type of parameters.
Stars: ✭ 85 (+77.08%)
Mutual labels:  decorator
routing
Aplus Framework Routing Library
Stars: ✭ 186 (+287.5%)
Mutual labels:  presenter
discord-nestjs
👾 NestJS package for discord.js
Stars: ✭ 173 (+260.42%)
Mutual labels:  decorator
typescript-lazy-get-decorator
Lazily evaluates a getter on an object and caches the returned value
Stars: ✭ 33 (-31.25%)
Mutual labels:  decorator
Decor.NET
A simple way to decorate a class with additional functionality using attributes.
Stars: ✭ 29 (-39.58%)
Mutual labels:  decorator
react-reflorp
Basically a simple ORM using Refetch, but the data is stored in Redux. Feel free to open an issue if you have a question or feature request.
Stars: ✭ 45 (-6.25%)
Mutual labels:  decorator
performance-decorator
🏇User behavior & Function execution tracking solution - 大型前端项目的用户行为跟踪,函数调用链分析,断点调试共享化和复用化实践
Stars: ✭ 39 (-18.75%)
Mutual labels:  decorator
typescript-retry-decorator
lightweight typescript retry decorator with 0 dependency.
Stars: ✭ 50 (+4.17%)
Mutual labels:  decorator
ngx-redux-core
The modern redux integration for Angular 6+
Stars: ✭ 32 (-33.33%)
Mutual labels:  decorator
python-makefun
Dynamically create python functions with a proper signature.
Stars: ✭ 62 (+29.17%)
Mutual labels:  decorator
yii2-presenter
Yii2 View Presenter
Stars: ✭ 13 (-72.92%)
Mutual labels:  presenter
textarea-autosize
Autosizes textarea to size of it's contents.
Stars: ✭ 73 (+52.08%)
Mutual labels:  decorator

Burgundy

Travis-CI Code Climate Test Coverage Gem Gem

A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 150 lines.

Installation

Add this line to your application's Gemfile:

gem 'burgundy'

And then execute:

$ bundle

Or install it yourself as:

$ gem install burgundy

Usage

First, define your wrapping class.

class UserPresenter < Burgundy::Item
end

Then you can instantiate it:

user = UserPresenter.new(User.first)

The Burgundy::Item has access to helper and route methods. Notice that the wrapper item is accessible through the Burgundy::Item#item method.

class UserPresenter < Burgundy::Item
  def profile_url
    routes.profile_url(item.username)
  end
end

You don't have to expose attributes; everything is delegated to the wrapped item.

To wrap an entire collection, just use the Burgundy::Collection class.

class WorkshopsController < ApplicationController
  def index
    @workshops = Burgundy::Collection.new(
      Workshop.sorted_by_name,
      WorkshopPresenter
    )
  end
end

or just call WorkshopPresenter.wrap(Workshop.sorted_by_name). Both ways return a Burgundy::Collection instance.

You may need to provide additional arguments to the item class. On your collection, all additional arguments will be delegated to the item classe, like the following example:

WorkshopPresenter.wrap(Workshop.all, current_user)
Burgundy::Collection.new(Workshop.all, WorkshopPresenter, current_user)

class WorkshopPresenter < Burgundy::Item
  def initialize(workshop, current_user)
    super(workshop)
    @current_user = current_user
  end
end

The query will be performed only when needed, usually on the view (easier to cache). The collection is an enumerable object and can be passed directly to the render method. Each item will be wrapped by the provided class.

<%= render @workshops %>

Route URLs may require the default url options. Burgundy try to get them from the following objects:

  • Rails.configuration.action_mailer.default_url_options
  • Rails.application.routes.default_url_options

So you can just put this on your environment file

config.action_controller.default_url_options = {
  host: "example.org"
}

You can map attributes into a hash; I use this strategy for using presenters on API responses (so I can skip adding yet another dependency to my project).

class UserPresenter < Burgundy::Item
  attributes :username, :name, :email

  def profile_url
    routes.profile_url(item.username)
  end
end

UserPresenter.new(User.first).attributes
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

UserPresenter.new(User.first).to_hash
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

UserPresenter.new(User.first).to_h
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

UserPresenter.new(User.first).as_json
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

Notice that as_json will ignore any options provided.

If you want to remap an attribute, provide a hash.

class UserPresenter < Burgundy::Item
  attributes :name, :email, :username => :login

  def profile_url
    routes.profile_url(item.username)
  end
end

UserPresenter.new(User.first).attributes
#=> {:login=>'johndoe', :name=>'John Doe', :email=>'[email protected]'}

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

License

Copyright (c) 2013 Nando Vieira

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].