Rspec Screenshot

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

Wednesday, November 18, 2015

JIT Inheritance

This flavor of the inheritance model allows our modular closures to have similar properties to the inheritance of classes. With it you can expect to have access to its super members as part of the call, just like you would with classes. In addition to the inheritance resulting from versioning, JIT inheritance presents a more complete scenario adding color to the picture painted by code injectors. The key takeaway here is this: Code Injectors are mix-ins that share a similar inheritance model with classes. You can version them to gain access to versioned inheritance or you can override its members to access an ancestor chain comprised of all previous tags. As always we will use some example code to illustrate:


    # 
    # Our Modular Closure
    # 
    Tag1 = trait :Tagger do
        def m1
            1
        end

        def m2
            :m2
        end
    end

    # 
    # Normal Versioned Injector inheritance
    # 
    Tagger do
        def other                   
            'other'                   # -- same ancestors as before
        end                                 
    end

    expect(Tagger().ancestors).to eql( [Tagger()] )

    # test it

    o  = Object.new.extend(Tagger())

    # inherited
    o.m1.should == 1
    o.m2.should == :m2

    # current
    o.other.should == 'other'


    #
    # JIT inheritance
    # 
    Tag2 = Tagger do
        def m1                        # The :m1 override invokes JIT inheritance
            super + 1                 # -- Tag1 is summoned into ancestor chain
        end                           # -- allows the use of super

        def m3                          
            'em3'
        end
    end

    # test it

    p = Object.new.extend(Tag2)

    # JIT inherited
    p.m1.should == 2

    # regular inheritance
    p.m2.should == :m2
    p.m3.should == 'em3'
    p.other.should == 'other'

    expect(Tagger().ancestors).to eql( [Tagger(), Tag1] )
    expect(Tag2.ancestors).to eql( [Tag2, Tag1] )
      

For more on this please see the rspec files in the project page or on the gem itself.

As always, Thank you kindly.

No comments:

Post a Comment