Sha256: ed1ada7116481c9d5ad2ac8381e44e47315d97021b9dbe969477ed3c8c2734e1
Contents?: true
Size: 1.51 KB
Versions: 1
Compression:
Stored size: 1.51 KB
Contents
= Basic Example Require the library. require 'paramix' Create a parametric mixin. module MyMixin include Paramix::Parametric parameterized do |params| public params[:name] do params[:value] end end end Create a class that uses the mixin and set the parameter. class X include MyMixin[:name => 'f', :value=>1] end Then the parameter is accessible. X.new.f.assert == 1 = Nested Parematric Mixins If we create another parametric mixin which depends on the first. module AnotherMixin include Paramix::Parametric include MyMixin[:name => 'f', :value=>1] parameterized do |params| public params[:name] do params[:value] end end end And a class for it. class Y include AnotherMixin[:name => 'g', :value=>2] end We can see that the parameters stay with their respective mixins. Y.new.f.assert == 1 Y.new.g.assert == 2 However if we do the same, but do not paramterize the first module then the including module also become parametric. module ThirdMixin #include Paramix::Parametric include MyMixin parameterized do |params| public params[:name].succ do params[:value] end end end And a class for it. class Z include ThirdMixin[:name => 'q', :value=>3] end We can see that the value of the parameter has propogated up to its ancestor parametric module. Z.new.q.assert == 3 Z.new.r.assert == 3
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
paramix-2.0.1 | qed/01_basic.rdoc |