Rspec Screenshot

Rspec Screenshot
Copyright © 2014, 2015 LHA. All rights reserved.

Wednesday, September 9, 2015

Simple Decorator

This feature of Jackbox allows you place decorators on individual methods without to much fuss, it gives you a nice alternative to #alias_method_chain in certain contexts. It also allows the use of #super and to revert back if necessary. This feature can also be used on instances even after an existing class decoration. Here is the code:

    # instance decoration

    one.decorate :foo do |arg|
      super() + ' ' + arg
    end' after').should == 
      'foo decoration after'

    one.undecorate :foo == 'foo decoration'


    #class decoration
    class One
      def foo

    class One
      decorate :foo do
        super() + ' ' + 'decoration'

    one = == 'foo decoration'
It also works like so:

    Object.decorate :inspect do
      super() + " is your object"
    end =~ /is your object/

No comments:

Post a Comment