Class: Serf::Util::RouteSet
- Inherits:
-
Object
- Object
- Serf::Util::RouteSet
- Defined in:
- lib/serf/util/route_set.rb
Overview
RouteSets hold routing information for ENV hashes to matched endpoints.
Class Method Summary collapse
-
.build(options = {}) ⇒ Object
Default factory method.
Instance Method Summary collapse
-
#add_route(options = {}) ⇒ Object
Connects a matcher (String or an Object implementing ===) to an endpoint.
-
#initialize(options = {}) ⇒ RouteSet
constructor
A new instance of RouteSet.
-
#match_routes(env = {}) ⇒ Array
List of endpoints that matched.
-
#size ⇒ Integer
Number of routes this RouteSet tracks.
Constructor Details
#initialize(options = {}) ⇒ RouteSet
Returns a new instance of RouteSet.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/serf/util/route_set.rb', line 12 def initialize(={}) @routes = {} @matchers = [] @regexp_matcher_factory = .fetch(:regexp_matcher_factory) { ::Serf::Util::RegexpMatcher } @route_endpoint_factory = .fetch(:route_endpoint_factory) { ::Serf::Util::RouteEndpoint } end |
Class Method Details
.build(options = {}) ⇒ Object
Default factory method.
76 77 78 |
# File 'lib/serf/util/route_set.rb', line 76 def self.build(={}) self.new end |
Instance Method Details
#add_route(options = {}) ⇒ Object
Connects a matcher (String or an Object implementing ===) to an endpoint.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/serf/util/route_set.rb', line 33 def add_route(={}) # We create our endpoint representation. endpoint = @route_endpoint_factory.build( handler: .fetch(:handler), action: .fetch(:action), message_parser: [:message_parser]) # Maybe we have an non-String matcher. Handle the Regexp case. # We only keep track of matchers if it isn't a string because # string matchers are just pulled out of routes by key lookup. matcher = .fetch :matcher matcher = @regexp_matcher_factory.build matcher if matcher.kind_of? Regexp @matchers << matcher unless matcher.is_a? String # We add the route (matcher+endpoint) into our routes @routes[matcher] ||= [] @routes[matcher] << endpoint end |
#match_routes(env = {}) ⇒ Array
Returns List of endpoints that matched.
56 57 58 59 60 61 62 63 64 |
# File 'lib/serf/util/route_set.rb', line 56 def match_routes(env={}) kind = env[:kind] routes = [] routes.concat Array(@routes[kind]) @matchers.each do |matcher| routes.concat Array(@routes[matcher]) if matcher === env end return routes end |
#size ⇒ Integer
Returns Number of routes this RouteSet tracks.
69 70 71 |
# File 'lib/serf/util/route_set.rb', line 69 def size return @routes.size end |