Module: SimpleMethodCallback

Defined in:
lib/simple_method_callback.rb,
lib/simple_method_callback/version.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods Classes: ArgumentTypeError

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/simple_method_callback.rb', line 5

def self.included(base)

  base.instance_eval do
    def around_action(around_method, only: nil)
      include ActiveSupport::Callbacks

      if only.nil?
        only = base.singleton_methods(false)
      elsif only.is_a?(Symbol) || only.is_a?(String)
        only = [only]
      end

      raise ArgumentTypeError, 'only argument type error' unless only.is_a?(Array)

      around_method_list ||= @@around_method_list ||= Set.new
      around_only_method_list ||= @@around_only_method_list ||= Set.new
      around_method_list << around_method
      around_only_method_list += only

      define_callbacks *around_only_method_list.map(&:to_sym)
      #TODO 苏铭轩 20200925 添加before 以及 after 即可实现 前置和后置回调
      # 以下这种方式比较low,需要借鉴优秀的项目是怎么去处理这种情况的
      class_variable_set('@@around_method_list', around_method_list)
      class_variable_set('@@around_only_method_list', around_only_method_list)

      extend ClassMethods
      prepend InstanceMethods
    end
  end
end