Sha256: 4d6f63d2de478ab9dd215b910cd94930ed227065067ed2805545b27545725598

Contents?: true

Size: 1018 Bytes

Versions: 8

Compression:

Stored size: 1018 Bytes

Contents

require 'active_record'

module FayeRails
  class Controller

    # Define callbacks into any ORM model.
    module ObserverFactory

      # Create
      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

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

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
faye-rails-2.0.1 lib/faye-rails/controller/observer_factory.rb
faye-rails-2.0.0 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.10 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.8 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.7 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.6 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.5 lib/faye-rails/controller/observer_factory.rb
faye-rails-1.0.4 lib/faye-rails/controller/observer_factory.rb