Class: Pine
- Inherits:
-
Object
- Object
- Pine
- Defined in:
- lib/sansom/pine.rb,
lib/sansom/pine/node.rb,
ext/sansom/pine/matcher.cpp
Defined Under Namespace
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize ⇒ Pine
constructor
A new instance of Pine.
-
#map_path(path, handler, key) ⇒ Object
map_path “/food”, Subsansom.new, :map map_path “/”, ObjectThatRespondsToCall.new, :get it’s also chainable.
-
#match(path, verb) ⇒ Object
match “/”, :get.
-
#path_comps(path) ⇒ Object
returns all non-root path components path_comps(“/my/path/”) => [“my”, “path”].
Constructor Details
Instance Method Details
#empty? ⇒ Boolean
20 21 22 |
# File 'lib/sansom/pine.rb', line 20 def empty? @root.leaf? end |
#map_path(path, handler, key) ⇒ Object
map_path “/food”, Subsansom.new, :map map_path “/”, ObjectThatRespondsToCall.new, :get it’s also chainable
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/sansom/pine.rb', line 34 def map_path path, handler, key @cache.clear node = (path == "/") ? @root : path_comps(path).inject(@root) { |n, comp| n << comp } if key == :mount && !handler.is_a?(Proc) if handler.singleton_class.include? Sansomable node.subsansoms << handler else node.rack_app = handler end else node.blocks[key] = handler end self end |
#match(path, verb) ⇒ Object
match “/”, :get
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sansom/pine.rb', line 53 def match path, verb return nil if empty? k = verb.to_s + path.to_s return @cache[k] if @cache.has_key? k matched_length = 0 matched_params = { :splat => [] } matched_wildcard = false # find a matching node walk = path_comps(path).inject @root do |n, comp| c = n[comp] break n if c.nil? matched_length += comp.length+1 if c.dynamic? matched_params.merge! c.mappings(comp) matched_params[:splat].push *c.splats(comp) matched_wildcard = true end c end return nil if walk.nil? remaining_path = path[matched_length..-1] match = walk.blocks[verb.downcase.to_sym] match ||= walk.subsansoms.detect { |i| i._pine.match remaining_path, verb } match ||= walk.rack_app return nil if match.nil? r = [match, remaining_path, path[0..matched_length-1], matched_params] @cache[k] = r unless matched_wildcard # Only cache static lookups (avoid huge memory usage) r end |
#path_comps(path) ⇒ Object
returns all non-root path components path_comps(“/my/path/”)
> [“my”, “path”]
27 28 29 |
# File 'lib/sansom/pine.rb', line 27 def path_comps path path.nil? || path.empty? ? [] : path[1..(path[-1] == "/" ? -2 : -1)].split('/') end |