Module: FayeRails::Controller::ObserverFactory

Defined in:
lib/faye-rails/controller/observer_factory.rb

Overview

Define callbacks into any ORM model.

Class Method Summary collapse

Class Method Details

.define(klass, method_name, &block) ⇒ Object

Create



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/faye-rails/controller/observer_factory.rb', line 10

def self.define(klass, method_name, &block)
  # Make a name for the callback module
  klass_callbacks_name = "#{klass.name}Callbacks"

  # Load the callback module if exists
  unless (klass_callbacks = ObserverFactory.observer(klass_callbacks_name))
    # Define callback module if one does not exist
    klass_callbacks = Object.const_set(klass_callbacks_name, Module.new)
  end

  # Add the method to the observer
  klass_callbacks.instance_eval do
    define_method(method_name, &block)
  end

  # Bind model callback
  klass.send(method_name, klass_callbacks.extend(klass_callbacks))
end

.observer(module_name) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/faye-rails/controller/observer_factory.rb', line 29

def self.observer(module_name)
  ref = Module.const_get(module_name)
  return ref if ref.is_a?(Module)
  nil
rescue
  nil
end