Class: Kazoo::Router::Route
- Inherits:
-
Object
- Object
- Kazoo::Router::Route
- Defined in:
- lib/kazoo/router/route.rb
Instance Method Summary collapse
- #app ⇒ Object
- #extract_params(path) ⇒ Object
-
#initialize(path, opts = {}) ⇒ Route
constructor
A new instance of Route.
- #named_params ⇒ Object
- #path(opts = {}) ⇒ Object
- #to_regexp ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(path, opts = {}) ⇒ Route
Returns a new instance of Route.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/kazoo/router/route.rb', line 5 def initialize(path, opts = {}) @named_params = [] @opts = opts.dup # Extract & delete the special options app_mode = @opts.delete(:app_mode) to = @opts.delete(:to) default_app = @opts.delete(:default_app) @app = to || default_app raise ArgumentError, 'You must supply an application/block' if !@app && !app_mode @path_prefix = @opts.delete(:path_prefix) @regex_prefix = @opts.delete(:regex_prefix) @opts.delete(:name_prefix) @opts.each { |k,v| next if k.is_a?(String) @opts.delete(k) @opts[k.to_s] = v } # Parse the path if path.is_a?(String) path = File.join(@path_prefix, path) if @path_prefix @passed_string = path @parts = path.split('/') @parts.shift if @parts[0].nil? regexp_parts = @parts.map { |part| # extracts the names of the path if matches = part.match(/^:([a-z]+[a-z0-9_]*)$/i) @named_params << matches[1].downcase "([^\/]+)" elsif part part else raise ArgumentError, 'Paths may not have blank parts' end } final_path = regexp_parts.join('/') elsif path.is_a?(Regexp) final_path = path.inspect final_path.gsub!(/(^\^)|(\$$)/, '') # Remove anchors final_path = File.join(@regex_prefix, file_path) if @regex_prefix else raise ArgumentError, "Invalid type passed to a route: #{path.class.name}. Must be either a string or regexp." end final_path = "/#{final_path}" unless final_path.match(%r"^\/") final_path = "#{final_path}/" unless final_path.match(%r"\/$") @matcher = Regexp.new(app_mode ? "^#{final_path}$" : "^#{final_path}" ) end |
Instance Method Details
#app ⇒ Object
72 73 74 |
# File 'lib/kazoo/router/route.rb', line 72 def app @app end |
#extract_params(path) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/kazoo/router/route.rb', line 80 def extract_params(path) if matches = @matcher.match(path) obj = @opts.dup named_params.each_with_index { |np, i| obj[np] = matches[i + 1] } obj end end |
#named_params ⇒ Object
76 77 78 |
# File 'lib/kazoo/router/route.rb', line 76 def named_params @named_params end |
#path(opts = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/kazoo/router/route.rb', line 88 def path(opts = {}) opts = @opts.merge(opts) raise "Path cannot be generated from a Regexp" unless @passed_string unused_params = @named_params - opts.keys.map { |key| key.to_s } raise ArgumentError, "You must supply the following params: #{unused_params.inspect}" if unused_params.size > 0 str = @passed_string.dup extra_keys = [] opts.keys.each { |key| if test = str.sub!(%r"(\:|\*)#{key}", opts[key].to_s) raise ArgumentError, "#{key} requires a value" if opts[key].nil? else extra_keys << key end } if extra_keys.size > 0 pstring = extra_keys.map { |key| "#{CGI.escape(key.to_s)}=#{CGI.escape(opts[key].to_s)}" unless opts[key].nil? }.compact.join('&') "#{str}?#{pstring}" else str end#if end |
#to_regexp ⇒ Object
68 69 70 |
# File 'lib/kazoo/router/route.rb', line 68 def to_regexp @matcher end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/kazoo/router/route.rb', line 64 def to_s @passed_string || @matcher.inspect end |