문제

I want to add an after_create callback to Role model by my plugin. So I can add an after_callback :my_private_method by class_eval as usual. I can add public method by defining it in InstanceMethods in the module that mixined in Role model.

But how I can add a private method my_private_method to Role model for using in after_create callback?

I know that this can be implemented by class_eval but is there any nicer solution?

도움이 되었습니까?

해결책

Oh it was really easy:

module RolePatch
  module InstanceMethods
    private    <<<<<<<<<<<<<<<<<<<<<<<<<<<<   It works like a charm.
    def my_private_method; end
  end

  def self.included(receiver)
    receiver.send :include, InstanceMethods

    receiver.class_eval do
      after_create :my_private_method
    end
  end
end

1.9.3p392 :017 > Role.first.private_methods.grep(/my_private_method/)
 => [:my_private_method]

So we can use private modifier in module InstanceMethods as usual.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top