Skip to content

StrangeMood/toolbox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

##decorator

Nifty tool for decorating any onject with module.

  require 'decorator'

  class Foo
    attr_accessor :name

    def initialize name
      @name = name
    end

    def some_method
      'Foo#some_method'
    end
  end

  module FooDecorator
    def another_method
      'FooDecorator#another_method'
    end

    def some_method
      "Decorated foo (#{super})"
    end
  end

Decorating instance of class

  foo = Foo.new('Local variable')
  foo = foo.decorate(FooDecorator)
  puts foo.some_method_undecorated
  puts foo.some_method
  puts foo.name
  Foo#some_method
  Decorated foo (Foo#some_method)
  Local variable

Decorating class itself

  foo = Foo.decorate(FooDecorator).new('Local variable')
  puts foo.some_method_undecorated
  puts foo.some_method
  puts foo.name
  Foo#some_method
  Decorated foo (Foo#some_method)
  Local variable

Undecorating

  bar = foo.undecorate
  puts bar.some_method
  puts bar.name
  Local variable
  Foo#some_method

##Installation:

Add to your Gemfile:

git "git://github.com/StrangeMood/toolbox.git" do
  gem "decorator"
end

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages