Rspec Screenshot

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

Monday, October 26, 2015

Injector Directives: collapse and rebuild

Continuing with our series: This time around we want to start a series on Injector Directives. We will begin by showing injector code pre-withdrawal and then its subsequent re-instatement. This is a feature that allows code to stay dormant until needed. Note: We use injector directives :collapse/:silence and :rebuild/:active. Take a look a the following example code:

    trait :PreFunction do           # Pre to main evaluation
      def _pre_
        puts '++++++++++'
      end
    end

    trait :PosFunction do           # Pos to main evaluation
      def _pos_
        puts '=========='
      end
    end

    # Our class

    class Model                     # Injector application is muted!

      inject PreFunction(:silence)  
      inject PosFunction(:silence) 

      def meth arg
        _pre_
        puts arg * arg
        _pos_
      end
    end

    # test

    obj = Model.new                 # functionality is silent !!
    obj.meth( 2 )
    # ==> 4


    PreFunction(:active)            # functionality is activated
    PosFunction(:active)

    # test
    
    obj.meth( 2 )
    # ==> ++++++++++
    # ==> 4
    # ==> ==========