Sha256: 2fa956ee53c2d07df863a49d384f261ecdaaf148eeda86656e8fcd35565dd96f

Contents?: true

Size: 718 Bytes

Versions: 12

Compression:

Stored size: 718 Bytes

Contents

module ApiResource
  
  module Observing
    
    extend ActiveSupport::Concern
    include ActiveModel::Observing
    
    # Redefine these methods to 
    included do
      %w( create save update destroy ).each do |method|
        alias_method_chain method, :observers
    end
    
    module InstanceMethods
      %w( create save update destroy ).each do |method|
        module_eval <<-EOE, __FILE__, __LINE__ + 1
          def #{method}_with_observers(*args)
            notify_observers(:before_#method)
            if result = #{method}_without_observers(*args)
              notify_observers(:after_#{method})
            end
            return result
          end
        EOE
      end
    end
    
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
api_resource-0.2.11 lib/api_resource/observing.rb
api_resource-0.2.10 lib/api_resource/observing.rb
api_resource-0.2.9 lib/api_resource/observing.rb
api_resource-0.2.8 lib/api_resource/observing.rb
api_resource-0.2.7 lib/api_resource/observing.rb
api_resource-0.2.6 lib/api_resource/observing.rb
api_resource-0.2.5 lib/api_resource/observing.rb
api_resource-0.2.4 lib/api_resource/observing.rb
api_resource-0.2.3 lib/api_resource/observing.rb
api_resource-0.2.2 lib/api_resource/observing.rb
api_resource-0.2.1 lib/api_resource/observing.rb
resource-0.1.0 lib/api_resource/observing.rb