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

#delete(path, handler_name) ⇒ Object



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

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

#get(path, handler_name) ⇒ Object



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

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

#patch(path, handler_name) ⇒ Object



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

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

#post(path, handler_name) ⇒ Object



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

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

#put(path, handler_name) ⇒ Object



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

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

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/deas/router.rb', line 53

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/deas/router.rb', line 42

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, from_url_path || from_path, proxy)
end

#url(name, path) ⇒ Object



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

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)


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

def url_for(name, *args)
  url = self.urls[name.to_sym]
  raise ArgumentError, "no route named `#{name.to_sym.inspect}`" unless 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