Module: ProconBypassMan::Callbacks

Included in:
Bypass::ProconToSwitch, Bypass::SwitchToProcon
Defined in:
lib/procon_bypass_man/support/callbacks.rb

Defined Under Namespace

Modules: ClassMethods Classes: Callback, CallbackChain

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



39
40
41
42
# File 'lib/procon_bypass_man/support/callbacks.rb', line 39

def self.included(mod)
  mod.singleton_class.attr_accessor :__callbacks
  mod.extend(ClassMethods)
end

Instance Method Details

#get_callbacks(kind) ⇒ Object

:nodoc:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/procon_bypass_man/support/callbacks.rb', line 93

def get_callbacks(kind) # :nodoc:
  # classに直接moduleをincludeしている場合
  if defined?(self.class.__callbacks) && !self.class.respond_to?(:callbacks)
    return self.class.__callbacks[kind.to_sym]
  end

  list =
    if self.class.callbacks.nil?
      []
    else
      self.class.callbacks.flat_map { |callback_mod|
        callback_mod.__callbacks && callback_mod.__callbacks[kind.to_sym]
      }.compact
    end
  if(self.class.respond_to?(:__callbacks) && chain = self.class.__callbacks[kind.to_sym])
    list << chain
  end
  table = {}
  table[:before] = list.flat_map { |x| x[:before] }.compact
  table[:after] = list.flat_map { |x| x[:after] }.compact
  table
end

#run_callbacks(kind, &block) ⇒ Object

TODO haltしたらcallbackを止める



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/procon_bypass_man/support/callbacks.rb', line 76

def run_callbacks(kind, &block)
  chains = get_callbacks(kind) or raise("unknown callback")
  if chains.nil? || chains.empty?
    return block.call
  end

  chains[:before]&.each do |chain|
    send chain.chain_method
  end
  result = block.call
  chains[:after]&.each do |chain|
    send chain.chain_method
  end

  return result
end