Sha256: 45ce8c96a9a97399ec26e8286579d28c37555f83a1c028caf56cb1301eb23525

Contents?: true

Size: 1.53 KB

Versions: 32

Compression:

Stored size: 1.53 KB

Contents

module Flexirest
  module Callbacks
    module ClassMethods
      def before_request(method_name = nil, &block)
        @before_callbacks ||= []
        if block
          @before_callbacks << block
        elsif method_name
          @before_callbacks << method_name
        end
      end

      def after_request(method_name = nil, &block)
        @after_callbacks ||= []
        if block
          @after_callbacks << block
        elsif method_name
          @after_callbacks << method_name
        end
      end

      def _callback_request(type, name, param)
        _handle_super_class_callbacks(type, name, param)
        @before_callbacks ||= []
        @after_callbacks ||= []
        callbacks = (type == :before ? @before_callbacks : @after_callbacks)
        callbacks.each do |callback|
          if callback.is_a? Symbol
            if self.respond_to?(callback)
              self.send(callback, name, param)
            else
              instance = self.new
              instance.send(callback, name, param)
            end
          else
            callback.call(name, param)
          end
        end
      end

      def _handle_super_class_callbacks(type, name, request)
        @parents ||= []
        @parents.each do |parent|
          parent._callback_request(type, name, request)
        end
      end

      def _parents
        @parents ||= []
      end

      def inherited(subclass)
        subclass._parents << self
        super
      end
    end

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

  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
flexirest-1.5.7 lib/flexirest/callbacks.rb
flexirest-1.5.6 lib/flexirest/callbacks.rb
flexirest-1.5.5 lib/flexirest/callbacks.rb
flexirest-1.5.4 lib/flexirest/callbacks.rb
flexirest-1.5.3 lib/flexirest/callbacks.rb
flexirest-1.5.2 lib/flexirest/callbacks.rb
flexirest-1.5.1 lib/flexirest/callbacks.rb
flexirest-1.5.0 lib/flexirest/callbacks.rb
flexirest-1.4.9 lib/flexirest/callbacks.rb
flexirest-1.4.8 lib/flexirest/callbacks.rb
flexirest-1.4.7 lib/flexirest/callbacks.rb
flexirest-1.4.6 lib/flexirest/callbacks.rb
flexirest-1.4.5 lib/flexirest/callbacks.rb
flexirest-1.4.4 lib/flexirest/callbacks.rb
flexirest-1.4.3 lib/flexirest/callbacks.rb
flexirest-1.4.2 lib/flexirest/callbacks.rb
flexirest-1.4.1 lib/flexirest/callbacks.rb
flexirest-1.4.0 lib/flexirest/callbacks.rb
flexirest-1.3.35 lib/flexirest/callbacks.rb
flexirest-1.3.34 lib/flexirest/callbacks.rb