Created
July 25, 2012 12:50
-
-
Save nnabeyang/3176015 to your computer and use it in GitHub Desktop.
複数のモジュールにあるStaticeMethodsを1つのクラスのクラスメソッドにまとめる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Concern | |
| def self.extended(base) | |
| base.instance_variable_set("@dependencies", []) | |
| end | |
| def append_features(base) | |
| if base.instance_variable_defined?("@dependencies") | |
| base.instance_variable_get("@dependencies") << self | |
| else | |
| super | |
| @dependencies.each {|dep| base.send(:include, dep)} | |
| base.extend const_get("ClassMethods") if const_defined?("ClassMethods") | |
| end | |
| end | |
| end | |
| module M | |
| extend Concern | |
| module ClassMethods | |
| def m1 | |
| puts "hello" | |
| end | |
| end | |
| end | |
| module M2 | |
| extend Concern | |
| include M | |
| module ClassMethods | |
| def m2 | |
| m1 | |
| end | |
| end | |
| end | |
| class C | |
| include M2 | |
| end | |
| C.m2#=> hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment