Class: N::ServerFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/n/server/filter.rb

Overview

Filter

A server serves client requests by feeding the request/request pair to a pipeline of processing filters. This is not a simple linear pipeline. Instead it is what we call a ‘folding’ (hierarchical) pipeline: each filter encapsulates the next. In effect, the pipeline is a generalized filter!

Design:

Filters are NOT singleton classes. This way we can assign one filter class to multiple resources, and keep statistics and metrics for each resource. A filter may contain state (attributes) for example metrics.

Direct Known Subclasses

App::AutoLoginFilter, App::Handler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(next_filter = nil) ⇒ ServerFilter

set the filters next.

example: LogFilter.new(TimeFilter.new(PageFilter.new))



39
40
41
# File 'lib/n/server/filter.rb', line 39

def initialize(next_filter = nil)
	@next_filter = next_filter
end

Instance Attribute Details

#next_filterObject (readonly)

the next filter in the pipeline.



32
33
34
# File 'lib/n/server/filter.rb', line 32

def next_filter
  @next_filter
end

Instance Method Details

#<<(next_filter = nil) ⇒ Object

set the filters next (next_filter).

example: LogFilter.new << TimeFilter.new << PageFilter.new



48
49
50
# File 'lib/n/server/filter.rb', line 48

def << (next_filter = nil)
	@next_filter = next_filter
end

#process(request) ⇒ Object

override this method to implement your filter.



54
55
56
57
58
59
60
61
62
63
# File 'lib/n/server/filter.rb', line 54

def process(request)
	# preprocessing comes here...

	# walk the pipeline
	return process_next(request)

	# postprocessing comes here...

	# return the result...
end

#process_next(request) ⇒ Object

process the next filter in the pipeline



67
68
69
70
71
72
73
# File 'lib/n/server/filter.rb', line 67

def process_next(request)
	if @next_filter
		return @next_filter.process(request)
	else
		return nil
	end
end