Module: Flexirest::Callbacks::ClassMethods

Defined in:
lib/flexirest/callbacks.rb

Instance Method Summary collapse

Instance Method Details

#_callback_request(type, name, param) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/flexirest/callbacks.rb', line 22

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

#_handle_super_class_callbacks(type, name, request) ⇒ Object



41
42
43
44
45
46
# File 'lib/flexirest/callbacks.rb', line 41

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

#_parentsObject



48
49
50
# File 'lib/flexirest/callbacks.rb', line 48

def _parents
  @parents ||= []
end

#after_request(method_name = nil, &block) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/flexirest/callbacks.rb', line 13

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

#before_request(method_name = nil, &block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/flexirest/callbacks.rb', line 4

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

#inherited(subclass) ⇒ Object



52
53
54
55
# File 'lib/flexirest/callbacks.rb', line 52

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