Class: CanvasSync::ClassCallbackExecutor

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Callbacks
Defined in:
lib/canvas_sync/class_callback_executor.rb

Overview

Helper/Hack class to allow calling ActiveSupport callbacks on a class instead of just on instances

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cls, env) ⇒ ClassCallbackExecutor

Returns a new instance of ClassCallbackExecutor.



10
11
12
13
14
15
16
17
# File 'lib/canvas_sync/class_callback_executor.rb', line 10

def initialize(cls, env)
  @callback_class = cls
  env.keys.each do |k|
    define_singleton_method(k) do
      env[k]
    end
  end
end

Instance Attribute Details

#callback_classObject (readonly)

Returns the value of attribute callback_class.



6
7
8
# File 'lib/canvas_sync/class_callback_executor.rb', line 6

def callback_class
  @callback_class
end

Class Method Details

.run_callbacks(cls, callback, env = {}, &blk) ⇒ Object



23
24
25
# File 'lib/canvas_sync/class_callback_executor.rb', line 23

def self.run_callbacks(cls, callback, env={}, &blk)
  new(cls, env).run_callbacks(callback, &blk)
end

.run_if_defined(cls, callback, *args, &blk) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/canvas_sync/class_callback_executor.rb', line 27

def self.run_if_defined(cls, callback, *args, &blk)
  if cls.respond_to?(:"_#{callback}_callbacks")
    run_callbacks(cls, callback, *args, &blk)
  else
    blk.call
  end
end

Instance Method Details

#clazzObject



19
20
21
# File 'lib/canvas_sync/class_callback_executor.rb', line 19

def clazz
  callback_class
end