Sha256: 48ff996335c21982c8273b5ecbae08012b00bf22010bb45735e23eb6c6def097

Contents?: true

Size: 1.21 KB

Versions: 14

Compression:

Stored size: 1.21 KB

Contents

require 'dm-core'

module DataMapper
  # Observers allow you to add callback hooks to DataMapper::Resource objects
  # in a separate class. This is great for separating out logic that is not
  # really part of the model, but needs to be triggered by a model, or models.
  module Observer

    def self.included(klass)
      klass.extend(ClassMethods)
    end

    module ClassMethods

      attr_accessor :observing

      def initialize
        self.observing = []
      end

      # Assign an Array of Class names to watch.
      #   observe User, Article, Topic
      def observe(*args)
        self.observing = args
      end

      def before(sym, &block)
        self.observing.each do |klass|
          klass.before(sym.to_sym, &block)
        end
      end

      def after(sym, &block)
        self.observing.each do |klass|
          klass.after(sym.to_sym, &block)
        end
      end

      def before_class_method(sym, &block)
        self.observing.each do |klass|
          klass.before_class_method(sym.to_sym, &block)
        end
      end

      def after_class_method(sym, &block)
        self.observing.each do |klass|
          klass.after_class_method(sym.to_sym, &block)
        end
      end

    end

  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
ardm-observer-1.2.0 lib/dm-observer/observer.rb
dm-observer-1.2.0 lib/dm-observer/observer.rb
dm-observer-1.2.0.rc2 lib/dm-observer/observer.rb
dm-observer-1.2.0.rc1 lib/dm-observer/observer.rb
dm-observer-1.1.0 lib/dm-observer/observer.rb
dm-observer-1.1.0.rc3 lib/dm-observer/observer.rb
dm-observer-1.1.0.rc2 lib/dm-observer/observer.rb
dm-observer-1.1.0.rc1 lib/dm-observer/observer.rb
dm-observer-1.0.2 lib/dm-observer/observer.rb
dm-observer-1.0.1 lib/dm-observer/observer.rb
dm-observer-1.0.0 lib/dm-observer/observer.rb
dm-observer-1.0.0.rc3 lib/dm-observer/observer.rb
dm-observer-1.0.0.rc2 lib/dm-observer/observer.rb
dm-observer-1.0.0.rc1 lib/dm-observer/observer.rb