Module: Yapper::Document::Callbacks

Extended by:
MotionSupport::Concern
Included in:
Yapper::Document
Defined in:
lib/yapper/document/callbacks.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disabled(&block) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/yapper/document/callbacks.rb', line 24

def self.disabled(&block)
  previous_value = Thread.current[:disabled_callbacks]
  Thread.current[:disabled_callbacks] = true

  block.call

  Thread.current[:disabled_callbacks] = previous_value
end

.disabled?Boolean

Returns:



33
34
35
# File 'lib/yapper/document/callbacks.rb', line 33

def self.disabled?
  !!Thread.current[:disabled_callbacks]
end

.postpone_callback(&block) ⇒ Object



37
38
39
# File 'lib/yapper/document/callbacks.rb', line 37

def self.postpone_callback(&block)
  Thread.current[:postponed_callbacks] << Proc.new(block)
end

.postpone_callbacks(&block) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/yapper/document/callbacks.rb', line 15

def self.postpone_callbacks(&block)
  Thread.current[:postponed_callbacks] = []

  block.call

  Thread.current[:postponed_callbacks].each(&:call)
  Thread.current[:postponed_callbacks] = nil
end

.postponed_callbacksObject



41
42
43
# File 'lib/yapper/document/callbacks.rb', line 41

def self.postponed_callbacks
  Thread.current[:postponed_callbacks]
end

Instance Method Details

#run_callback(hook, operation) ⇒ Object



55
56
57
58
59
# File 'lib/yapper/document/callbacks.rb', line 55

def run_callback(hook, operation)
  self.class.send("#{hook}_#{operation}_callbacks").each do |method|
    self.send(method)
  end
end

#run_callbacks(operation, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yapper/document/callbacks.rb', line 61

def run_callbacks(operation, &block)
  return yield if Yapper::Document::Callbacks.disabled?

  if run_callback('before', operation)
    block.call
  end

  callback_proc = Proc.new do
    run_callback('after', operation)
  end

  if Yapper::Document::Callbacks.postponed_callbacks
    Yapper::Document::Callbacks.postpone_callback(&callback_proc)
  else
    callback_proc.call
  end
end