Class: Rad::AliasRouter
- Includes:
- AbstractRouter
- Defined in:
- lib/rad/router/routers/alias_router.rb
Defined Under Namespace
Classes: TreeHash
Instance Method Summary collapse
- #add(als, options = {}) ⇒ Object
- #decode(path, params) ⇒ Object
- #encode(klass, method, params) ⇒ Object
- #encode_method(route_method) ⇒ Object
-
#initialize ⇒ AliasRouter
constructor
A new instance of AliasRouter.
Constructor Details
#initialize ⇒ AliasRouter
Returns a new instance of AliasRouter.
9 10 11 |
# File 'lib/rad/router/routers/alias_router.rb', line 9 def initialize @route_methods, @paths, @classes = {}, TreeHash.new, {} end |
Instance Method Details
#add(als, options = {}) ⇒ Object
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 |
# File 'lib/rad/router/routers/alias_router.rb', line 13 def add als, = {} # parsing options # als, options = als.to_s, options.to_openobject als = als.to_s als.must =~ /^\// klass = .delete(:class) || raise("no class!") method = .delete(:method) || raise("no method!") method = method.must_be.a Symbol url_root = parse_url_root prefix = parse_prefix raise "unknown options :#{.to_h.keys.join(': ')}!" unless .empty? raise "you can't use '/' alias with :url_root!" if als == '/' and url_root # logic = { class: klass, method: method, path: als, url_root: url_root, prefix: prefix } route_methods["#{als[1..-1]}_path"] = (classes[klass] ||= {})[method] = parts = als[1..-1].split('/') parts << '' if parts.empty? tree_iterator = paths parts.reverse.each do |part| tree_iterator = (tree_iterator[part] ||= TreeHash.new) raise "alias '#{als}' conflicted with another alias!" if tree_iterator. end tree_iterator. = end |
#decode(path, params) ⇒ Object
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 89 90 91 |
# File 'lib/rad/router/routers/alias_router.rb', line 64 def decode path, params parts = path[1..-1].split('/') parts << '' if parts.empty? tree_iterator = paths # checking for first part part = parts.pop tree_iterator = tree_iterator[part] return nil unless tree_iterator # checking for exact match while part = parts.pop tmp = tree_iterator[part] unless tmp parts << part break end tree_iterator = tmp end = tree_iterator. decode_prefix_params! parts, params, decode_url_root! parts, return [:class], [:method], params end |
#encode(klass, method, params) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rad/router/routers/alias_router.rb', line 51 def encode klass, method, params if = classes[klass].try(:[], method) path = [:path] path = encode_prefix_params! path, params, path = encode_url_root! path, return path, params else nil end end |
#encode_method(route_method) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/rad/router/routers/alias_router.rb', line 93 def encode_method route_method if = route_methods[route_method] return [:class], [:method] else nil end end |