Sha256: 01873c91ac588588d362af78c2b1765a5b66553623ac6e115f3fb519a3a9beb5
Contents?: true
Size: 1.9 KB
Versions: 2
Compression:
Stored size: 1.9 KB
Contents
# HoboSupport - Module extensions >> require 'hobosupport' >> HoboSupport::VERSION => "0.1" ## Module#included_in_class_callbacks Bit involved, this one :-) When a module is included in a class, it gets a callback on the `included` method, with the class passed as argument. However, if a module M2 is included in M1, and a class C includes M1, then M2 never gets to know about C. So, for example, M2 could not `alias_method_chain` a class method on C. `included_in_class_callbacks` makes it easy to implement a notification from M1 to M2, so that M2 does have full access to the class. All you do is insert a call to `included_in_class_callbacks(base)` as the end of the module's `self.included` method. (Note we're using the metaid extensions here too) >> module M2 def self.included_in_class(klass) klass.metaclass_eval do def name_with_shouting; name_without_shouting.upcase; end alias_method_chain :name, :shouting end end end module M1 def self.included(base) included_in_class_callbacks(base) end include M2 end class C def self.name "my name is C" end include M1 end C.name => "MY NAME IS C" ## Module#interiting_attr_accessor Only for use on classes. Like `attr_accessor`, but the attribute is looked up on the superclass if not defined on the receiving class. In other words, the superclass defines a default that subclasses can override. >> class A class << self inheriting_attr_reader :name end @name = "Andy" end class B < A; end `B` has the same name as its superclass `A` >> A.name => "Andy" >> B.name => "Andy" Now we change the name of `B`. `A` retains it's existing name. >> class B; @name = "Bob"; end >> B.name => "Bob" >> A.name => "Andy"
Version data entries
2 entries across 2 versions & 2 rubygems
Version | Path |
---|---|
hobo-support-0.1 | test/hobosupport/module.rdoctest |
hobosupport-0.1 | test/hobosupport/module.rdoctest |