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

    one.foo(' after').should == 
      'foo decoration after'

    one.undecorate :foo

    one.foo.should == 'foo decoration'




        

    #class decoration
    
    class One
      def foo
        'foo'
      end
    end

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

    one = One.new
    one.foo.should == 'foo decoration'
      
It also works like so:

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

No comments:

Post a Comment