Sha256: 79ca744745e769cbc8c51e3270bf796a1fe2eea81c0a2eeecd8cfd59ab0b4025

Contents?: true

Size: 1.36 KB

Versions: 60

Compression:

Stored size: 1.36 KB

Contents

require 'active_support/callbacks'

module JSONAPI
  module Callbacks
    def self.included(base)
      base.class_eval do
        include ActiveSupport::Callbacks
        base.extend ClassMethods
      end
    end

    module ClassMethods
      def define_jsonapi_resources_callbacks(*callbacks)
        options = callbacks.extract_options!
        options = {
          only: [:before, :around, :after]
        }.merge!(options)

        types = Array(options.delete(:only))

        callbacks.each do |callback|
          define_callbacks(callback, options)

          types.each do |type|
            send("_define_#{type}_callback", self, callback)
          end
        end
      end

      private

      def _define_before_callback(klass, callback) #:nodoc:
        klass.define_singleton_method("before_#{callback}") do |*args, &block|
          set_callback(:"#{callback}", :before, *args, &block)
        end
      end

      def _define_around_callback(klass, callback) #:nodoc:
        klass.define_singleton_method("around_#{callback}") do |*args, &block|
          set_callback(:"#{callback}", :around, *args, &block)
        end
      end

      def _define_after_callback(klass, callback) #:nodoc:
        klass.define_singleton_method("after_#{callback}") do |*args, &block|
          set_callback(:"#{callback}", :after, *args, &block)
        end
      end
    end
  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
jsonapi-resources-0.10.7 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.6 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.5 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.4 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.12 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.3 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.2 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.11 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.1 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta9 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta8 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta7 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta6 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.10 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.9 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta5 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.8 lib/jsonapi/callbacks.rb
jsonapi-resources-0.10.0.beta4 lib/jsonapi/callbacks.rb
jsonapi-resources-0.9.7 lib/jsonapi/callbacks.rb