2014-07-15 3 views
1

모듈을 alias_method_chain에 포함시키려는 모듈을 원합니다. 여기에 내가 쓴 방법입니다alias_method_chain in module

module MyModule 
    self.included(base) 
    base.class_eval do 
     alias_method_chain :perform, :chain 
    end 
    end 
    def perform_with_chain(opts) 
    #Do some stuffs 
    perform_without_chain(opts) 
    #Do some other stuffs 
    end 
end 

class SomeClass 
    include MyModule 
    def perform(opts) 
    end 
end 

을하지만이 모듈이 포함되어있는 경우에는 perform 방법은 아직 SomeClass에 정의되지 않은, 때문에 오류가 발생합니다 하나는이 패턴을 작성해야하는 방법

in `alias_method': undefined method `perform' for class `SomeClass' (NameError) 

을 별칭 체인이 완벽하게 작동합니까?

+0

일부 사용자는 모듈을 어디에 포함할지주의해야합니다. 더 나은 해결책이 있습니까? –

답변

2

다음을 포함하면 perform이 정의됩니다.

class SomeClass 
    def perform(opts) 
    end 
    include MyModule 
end 
당신은 따라서이 것을 요구, 모듈이 보석에 번들 될 수 있기 때문에이`은`SomeClass` 선언의 _end_에 MyModule`를 포함 ...
+0

내가이 일을하지 않으려는 쓸 수 – muichkine