Sha256: 2cf9d4a78d1753f548e76a846a1582e12ca04fbe7fc548e47a31ff42dae09944

Contents?: true

Size: 1.95 KB

Versions: 7

Compression:

Stored size: 1.95 KB

Contents

== Module#class_inheritor

  require 'facets/module/class_inheritor'

Subclass with inheritor

    c = Class.new do
      class_inheritor :koko, [], :+
      koko! << 1
    end

    d = Class.new(c) do
      class_inheritor :koko, [], :+
      koko! << 2
    end

We can see that

    c.koko!.assert == [1]
    c.koko.assert  == [1]

    d.koko!.assert == [2]
    d.koko.assert  == [1,2]

Subclass without class_inheritor

    c = Class.new do
      class_inheritor :koko, [], :+
      koko! << 1
    end

    d = Class.new(c)

Likewise

    c.koko!.assert == [1]
    c.koko.assert  == [1]

    d.koko!.assert == []
    d.koko.assert  == [1]


Include module with class_inheritor

    c = Class.new do
      class_inheritor :x, {}, :merge
      x![:a] = 1
    end

    m = Module.new do
      class_inheritor :x, {}, :merge
      x![:b] = 2
    end

    d = Class.new(c) do
      include m
      class_inheritor :x, {}, :merge
      x![:c] = 3
    end

Then

    c.x.assert == {:a=>1}

    m.x[:b].assert == 2

    d.x.assert == {:a=>1,:b=>2,:c=>3}

    c.x[:a].assert == 1
    c.x[:b].assert == nil
    c.x[:c].assert == nil

    d.x[:a].assert == 1
    d.x[:b].assert == 2
    d.x[:c].assert == 3

And

    d.x![:d] = 4
    d.x[:d].assert == 4


Using #concat as the class_inheritor operator

    c = Class.new do
      class_inheritor :relations, [], :concat
    end

    d = Class.new(c) do
      #class_inheritor :relations, [], :concat
    end

    c.relations! << 1
    c.relations! << 2
    d.relations! << 3

Notice

    c.relations.assert == [1,2]
    d.relations.assert == [1,2,3]


On Module

    m = Module.new do
      class_inheritor :koko, [], :+
      koko! << 1
    end

    c1 = Class.new do
      include m
      #class_inheritor :koko, [], :+
      koko! << 2
      koko! << 3
    end

    c2 = Class.new do
      include m
      #class_inheritor :koko, [], :+
      koko! << 4
    end

    m.koko.assert  == [1]
    c1.koko.assert == [1,2,3]
    c2.koko.assert == [1,4]

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
facets-3.0.0 demo/core/module/class_inheritor.rdoc
facets-2.9.3 qed/core/module/class_inheritor.rdoc
facets-2.9.2 qed/core-uncommon/module/class_inheritor.rdoc
facets-2.9.1 qed/core-uncommon/module/class_inheritor.rdoc
facets-2.9.0 qed/tour/module/class_inheritor.rdoc
facets-2.9.0.pre.2 qed/tour/module/class_inheritor.rdoc
facets-2.9.0.pre.1 qed/tour/module/class_inheritor.rdoc