Class: Rad::Router::AliasRouter

Inherits:
Object
  • Object
show all
Includes:
AbstractRouter
Defined in:
lib/rad/router/_alias_router.rb

Defined Under Namespace

Classes: TreeHash

Instance Method Summary collapse

Constructor Details

#initializeAliasRouter

Returns a new instance of AliasRouter.



8
9
10
# File 'lib/rad/router/_alias_router.rb', line 8

def initialize
  @route_methods, @paths, @classes = {}, TreeHash.new, {}
end

Instance Method Details

#add(als, options = {}) ⇒ Object



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
# File 'lib/rad/router/_alias_router.rb', line 12

def add als, options = {}
  # parsing options
  # als, options = als.to_s, options.to_openobject
  options.validate_options! :class_name, :method, :url_root, :prefix

  als = als.to_s
  als.must =~ /^\//
  class_name = options.delete(:class_name) || raise("no class name!")
  class_name.must_be.a String
  method = options.delete(:method) || raise("no method!")
  method = method.must_be.a Symbol
  url_root = parse_url_root options
  prefix = parse_prefix options

  raise "you can't use '/' alias with :url_root!" if als == '/' and url_root

  # logic
  meta = Meta.new
  meta.merge!(
    class_name: class_name,
    method: method,
    path: als,
    url_root: url_root,
    prefix: prefix
  )

  route_methods["#{als[1..-1]}_path"] = meta

  (classes[class_name] ||= {})[method] = meta


  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 ('#{tree_iterator.meta[:path]}')!" if tree_iterator.meta
  end
  tree_iterator.meta = meta
end

#decode(path, params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rad/router/_alias_router.rb', line 71

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

  meta = tree_iterator.meta

  # url_root
  url_root = meta[:url_root]
  return nil if url_root and "/#{parts.shift}" != url_root

  # prefix
  prefix = meta[:prefix]
  decode_prefix_params! parts, params, prefix if prefix

  return meta.get_class, meta[:method], params
end

#encode(klass, method, params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rad/router/_alias_router.rb', line 54

def encode klass, method, params
  if meta = classes[klass.name].try(:[], method)
    path = meta[:path]

    # prefix
    prefix = meta[:prefix]
    path = encode_prefix_params! path, params, prefix if prefix

    url_root = meta[:url_root]
    params[:url_root] = url_root if url_root

    return path, params
  else
    nil
  end
end

#encode_method(route_method, has_id) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rad/router/_alias_router.rb', line 105

def encode_method route_method, has_id
  if meta = route_methods[route_method]
    return meta.get_class, meta[:method]
  else
    nil
  end
end