Skip to content

gabriel-dehan/SimpleDecorator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-decorator -v
# => 0.2.0

SimpleDecorator allows you to quickly implement Decorators in your application.

A true decorator

SimpleDecorator decorators are true decorators :

  • They delegate unknown methods to the underlying objects

    class User
      def foo
        'bar'
      end
    end
    
    class Decorator < SimpleDecorator
    
    end
    
    user_decorator = Decorator.new(User.new)
    user_decorator.foo
    # => 'bar'
  • They can be stacked infinitely

      class User
        def initialize; @count = 1 end
    
        def count
          @count
        end
      end
    
      class CountDecorator < SimpleDecorator
       def count
         @component.count + 1
       end
      end
    
      class TripleCountDecorator < SimpleDecorator
        def count
          @component.count * 3
        end
      end
    
      user = User.new
      user.count
      # => 1
    
      CountDecorator.new(CountDecorator.new(user)).count
      # => 3
    
      TripleCountDecorator.new(CountDecorator.new(user)).count
      # => 6
  • They are fully transparent

      class User; end
      class Decorator < SimpleDecorator; end
      class OtherDecorator < SimpleDecorator; end
    
      user = User.new
      decorator = Decorator.new(OtherDecorator.new(user))
      decorator.class
      # => User
      decorator.instance_of? User
      # => true
      decorator == user
      # => true

Installation

gem install simple-decorator

Usage

To create a decorator, simply inherit from SimpleDecorator.

  class Decorator < SimpleDecorator
  end

Then, you just need to wrap your object inside your Decorator class

  class Foobar; end
  # Decorator#new takes the instance you want to decorate as an argument
  Decorator.new(Foobar.new)

It will give you access to the following instance methods : source, decorated which will return decorated object.

Inside your Decorator class, you can access the decorated object through the instance variable @component

  class Foobar
    def foo
      'bar'
    end
  end

  class Decorator < SimpleDecorator
    def foo
      'foo' + @component.foo
    end
  end

  Decorator.new(Foobar.new).foo
  # => 'foobar'

Misc

'Licensed' under WTFPL

About

A gem allowing simple, true decorators creation in Ruby

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages