0.0
No commit activity in last 3 years
No release in over 3 years
Minimal decoration of Ruby objects using a "decorate" method.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

mini_decorator

Gem Version

Minimal decoration of Ruby objects using a decorate method. The idea is to create the thinnest possible layer of abstraction between presentational methods and 'work' methods. I was inspired by gems such as Draper, but felt like they were doing too much.

Installation

Run gem install mini_decorator or add mini_decorator to your Gemfile.

gem 'mini_decorator'

Usage

require 'mini_decorator'

#
# This is where our presentational methods live
#
class PersonDecorator
  def name(person)
    "#{person.first_name} #{person.last_name}"
  end
end

#
# Imagine that this is an ActiveRecord model or something...
#
class Person
  include MiniDecorator.new(PersonDecorator.new)

  def first_name
    'John'
  end

  def last_name
    'Smith'
  end
end

person = Person.new
person.decorate(:name)
# => "John Smith"

#
# If a decorator isn't defined, MiniDecorator attempts to call the method on the model directly
#
person.decorate(:first_name)
# => "John"

#
# If the given item isn't a decorator or method, a NoMethodError is raised
#
person.decorate(:nonexistant)
# => NoMethodError raised

License

MIT