Class: Deas::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/deas/router.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



11
12
13
14
# File 'lib/deas/router.rb', line 11

def initialize(&block)
  @urls, @routes = {}, []
  self.instance_eval(&block) if !block.nil?
end

Instance Attribute Details

#routesObject

Returns the value of attribute routes.



9
10
11
# File 'lib/deas/router.rb', line 9

def routes
  @routes
end

#urlsObject

Returns the value of attribute urls.



9
10
11
# File 'lib/deas/router.rb', line 9

def urls
  @urls
end

Instance Method Details

#base_url(value = nil) ⇒ Object



21
22
23
24
# File 'lib/deas/router.rb', line 21

def base_url(value = nil)
  @base_url = value if !value.nil?
  @base_url
end

#delete(path, handler_name) ⇒ Object



48
# File 'lib/deas/router.rb', line 48

def delete(path, handler_name); self.route(:delete, path, handler_name); end

#get(path, handler_name) ⇒ Object



44
# File 'lib/deas/router.rb', line 44

def get(path, handler_name);    self.route(:get,    path, handler_name); end

#patch(path, handler_name) ⇒ Object



47
# File 'lib/deas/router.rb', line 47

def patch(path, handler_name);  self.route(:patch,  path, handler_name); end

#post(path, handler_name) ⇒ Object



45
# File 'lib/deas/router.rb', line 45

def post(path, handler_name);   self.route(:post,   path, handler_name); end

#prepend_base_url(url_path) ⇒ Object



26
27
28
# File 'lib/deas/router.rb', line 26

def prepend_base_url(url_path)
  "#{base_url}#{url_path}"
end

#put(path, handler_name) ⇒ Object



46
# File 'lib/deas/router.rb', line 46

def put(path, handler_name);    self.route(:put,    path, handler_name); end

#redirect(from_path, to_path = nil, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/deas/router.rb', line 61

def redirect(from_path, to_path = nil, &block)
  to_url = self.urls[to_path]
  if to_path.kind_of?(::Symbol) && to_url.nil?
    raise ArgumentError, "no url named `#{to_path.inspect}`"
  end
  proxy = Deas::RedirectProxy.new(to_url || to_path, &block)

  from_url = self.urls[from_path]
  from_url_path = from_url.path if from_url
  add_route(:get, from_url_path || from_path, proxy)
end

#route(http_method, from_path, handler_class_name) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/deas/router.rb', line 50

def route(http_method, from_path, handler_class_name)
  if self.view_handler_ns && !(handler_class_name =~ /^::/)
    handler_class_name = "#{self.view_handler_ns}::#{handler_class_name}"
  end
  proxy = Deas::RouteProxy.new(handler_class_name)

  from_url = self.urls[from_path]
  from_url_path = from_url.path if from_url
  add_route(http_method, prepend_base_url(from_url_path || from_path), proxy)
end

#url(name, path) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/deas/router.rb', line 30

def url(name, path)
  if !path.kind_of?(::String)
    raise ArgumentError, "invalid path `#{path.inspect}` - "\
                         "can only provide a url name with String paths"
  end
  add_url(name.to_sym, path)
end

#url_for(name, *args) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/deas/router.rb', line 38

def url_for(name, *args)
  url = self.urls[name.to_sym]
  raise ArgumentError, "no route named `#{name.to_sym.inspect}`" unless url
  prepend_base_url(url.path_for(*args))
end

#view_handler_ns(value = nil) ⇒ Object



16
17
18
19
# File 'lib/deas/router.rb', line 16

def view_handler_ns(value = nil)
  @view_handler_ns = value if !value.nil?
  @view_handler_ns
end