Class: Citrus::Common::Service::FilterService

Inherits:
Object
  • Object
show all
Defined in:
lib/citrus/common/service/filter_service.rb

Overview

FilterService

Instance Method Summary collapse

Constructor Details

#initializeFilterService

Initialize the service



19
20
21
22
# File 'lib/citrus/common/service/filter_service.rb', line 19

def initialize
  @befores = []
  @afters = []
end

Instance Method Details

#after(filter) ⇒ Object

Add after filter into the filter chain

Parameters:

  • filter (#call)


34
35
36
# File 'lib/citrus/common/service/filter_service.rb', line 34

def after filter
  @afters.unshift filter
end

#after_filter(err, msg, session, resp, &block) ⇒ Object

Do the after filter chain

Parameters:

  • err (Object)
  • msg (Hash)
  • session (Object)
  • resp (Hash)


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/citrus/common/service/filter_service.rb', line 69

def after_filter err, msg, session, resp, &block
  index = 0

  next_p = Proc.new { |err|
    if index >= @afters.length
      block_given? and yield err
      return
    end

    handler = @afters[index]
    index += 1

    if handler.respond_to? :call
      handler.call err, msg, session, resp, &next_p
    else
      next_p.call Exception.new 'invalid after filter'
    end
  }
  next_p.call err
end

#before(filter) ⇒ Object

Add before filter into the filter chain

Parameters:

  • filter (#call)


27
28
29
# File 'lib/citrus/common/service/filter_service.rb', line 27

def before filter
  @befores << filter
end

#before_filter(msg, session, &block) ⇒ Object

Do the before filter chain

Parameters:

  • msg (Hash)
  • session (Object)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/citrus/common/service/filter_service.rb', line 42

def before_filter msg, session, &block
  index = 0

  next_p = Proc.new { |err, resp, args|
    if err || index >= @befores.length
      block_given? and yield err, resp, args
      return
    end

    handler = @befores[index]
    index += 1

    if handler.respond_to? :call
      handler.call msg, session, &next_p
    else
      next_p.call Exception.new 'invalid before filter'
    end
  }
  next_p.call
end