Class: Deas::Router

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

Defined Under Namespace

Classes: HandlerProxies, RequestType

Constant Summary collapse

DEFAULT_REQUEST_TYPE_NAME =
'default'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

Returns a new instance of Router.



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

def initialize(&block)
  @request_types = []
  @urls, @routes, @definitions = {}, [], []
  default_request_type_name(DEFAULT_REQUEST_TYPE_NAME)
  escape_query_value{ |v| Rack::Utils.escape(v) }
  self.instance_eval(&block) if !block.nil?
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



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

def definitions
  @definitions
end

#escape_query_value_procObject (readonly)

Returns the value of attribute escape_query_value_proc.



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

def escape_query_value_proc
  @escape_query_value_proc
end

#request_typesObject (readonly)

Returns the value of attribute request_types.



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

def request_types
  @request_types
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

#urlsObject (readonly)

Returns the value of attribute urls.



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

def urls
  @urls
end

Instance Method Details

#add_request_type(name, &proc) ⇒ Object



74
75
76
# File 'lib/deas/router.rb', line 74

def add_request_type(name, &proc)
  @request_types << RequestType.new(name, proc)
end

#apply_definitions!Object



104
105
106
107
108
109
110
# File 'lib/deas/router.rb', line 104

def apply_definitions!
  self.definitions.each do |(type, args, block)|
    self.send("apply_#{type}", *args, &block)
  end
  self.definitions.clear
  true
end

#base_url(value = nil) ⇒ Object



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

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

#default_request_type_name(value = nil) ⇒ Object



69
70
71
72
# File 'lib/deas/router.rb', line 69

def default_request_type_name(value = nil)
  @default_request_type = RequestType.new(value) if !value.nil?
  @default_request_type.name
end

#delete(path, *args) ⇒ Object



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

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

#escape_query_value(&block) ⇒ Object

Raises:

  • (ArgumentError)


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

def escape_query_value(&block)
  raise(ArgumentError, "no block given") unless block
  @escape_query_value_proc = block
end

#get(path, *args) ⇒ Object



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

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

#not_found(from_path, body = nil) ⇒ Object



99
100
101
102
# File 'lib/deas/router.rb', line 99

def not_found(from_path, body = nil)
  respond_with_args = [404, {}, body || 'Not Found']
  self.definitions.push([:respond_with, [from_path, respond_with_args], nil])
end

#patch(path, *args) ⇒ Object



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

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

#post(path, *args) ⇒ Object



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

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

#prepend_base_url(url_path) ⇒ Object



41
42
43
# File 'lib/deas/router.rb', line 41

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

#put(path, *args) ⇒ Object



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

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

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



94
95
96
97
# File 'lib/deas/router.rb', line 94

def redirect(from_path, to_path = nil, &block)
  self.definitions.push([:redirect, [from_path, to_path], block])
  true
end

#request_type_name(request) ⇒ Object

ideally someday the request should just know its request type



79
80
81
# File 'lib/deas/router.rb', line 79

def request_type_name(request)
  (self.request_types.detect{ |rt| rt.proc.call(request) } || @default_request_type).name
end

#route(http_method, from_path, *args) ⇒ Object



89
90
91
92
# File 'lib/deas/router.rb', line 89

def route(http_method, from_path, *args)
  self.definitions.push([:route, [http_method, from_path, *args], nil])
  true
end

#set_base_url(value) ⇒ Object



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

def set_base_url(value)
  @base_url = value
end

#url(name, path, options = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/deas/router.rb', line 45

def url(name, path, options = nil)
  if !name.kind_of?(::Symbol)
    raise ArgumentError, "invalid name `#{name.inspect}` - "\
                         "named urls must be defined with Symbol names"
  end
  if !path.kind_of?(::String)
    raise ArgumentError, "invalid path `#{path.inspect}` - "\
                         "named urls must be defined with String paths"
  end
  add_url(name, path, options || {})
end

#url_for(name, *args) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/deas/router.rb', line 57

def url_for(name, *args)
  url = self.urls[name.to_sym]
  raise ArgumentError, "no route named `#{name.to_sym.inspect}`" unless url
  begin
    prepend_base_url(url.path_for(*args))
  rescue Deas::Url::NonHashParamsError => err
    raise ArgumentError, "url param values must be passed as a Hash"
  rescue Deas::Url::EmptyNamedValueError => err
    raise ArgumentError, err.message
  end
end

#validate!Object



112
113
114
115
116
# File 'lib/deas/router.rb', line 112

def validate!
  self.apply_definitions!
  self.routes.each(&:validate!)
  true
end

#view_handler_ns(value = nil) ⇒ Object



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

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