Class: Rad::Router
Instance Attribute Summary collapse
-
#format_processor ⇒ Object
readonly
Returns the value of attribute format_processor.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
- #decode(path, params) ⇒ Object
- #dont_persist_params(&block) ⇒ Object
- #encode(klass, method, params = {}) ⇒ Object
- #encode_method(route_method) ⇒ Object
-
#initialize(class_variable, routes = {default_router: SimpleRouter.new}, format_processor = DefaultFormatProcessor.new) ⇒ Router
constructor
A new instance of Router.
- #persist_params(&block) ⇒ Object
- #persist_params? ⇒ Boolean
- #persistent_params ⇒ Object
- #skip(path_regexp = nil) ⇒ Object
- #url_for(*args) ⇒ Object
- #url_for_class(klass, method, params) ⇒ Object
- #url_for_path(path, params) ⇒ Object
Constructor Details
#initialize(class_variable, routes = {default_router: SimpleRouter.new}, format_processor = DefaultFormatProcessor.new) ⇒ Router
Returns a new instance of Router.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/rad/router/core/router.rb', line 10 def initialize class_variable, routes = {default_router: SimpleRouter.new}, format_processor = DefaultFormatProcessor.new @class_variable, @format_processor = class_variable, format_processor @routes = Dictionary.new routes.each do |k, v| k.must_be.present v.must_be.present v.must.respond_to(:decode) v.must.respond_to(:encode) @routes[k] = v end end |
Instance Attribute Details
#format_processor ⇒ Object (readonly)
Returns the value of attribute format_processor.
8 9 10 |
# File 'lib/rad/router/core/router.rb', line 8 def format_processor @format_processor end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
8 9 10 |
# File 'lib/rad/router/core/router.rb', line 8 def routes @routes end |
Instance Method Details
#decode(path, params) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rad/router/core/router.rb', line 52 def decode path, params path.first.must == '/' # params = params.to_openobject throw :skip, true if skip.any?{|path_regexp| path_regexp =~ path} path, format = format_processor.remove_format path if format_processor result = nil routes.each do |name, route| result = route.decode path, params break if result end raise "no route for '#{safe_workspace.path}' request!" unless result klass, method, params = result method ||= config.default_method params.must_be.defined method.must_be.a Symbol raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method raise "Invalid route! You try to call protected method '#{method}' for '#{path}'!" unless klass.public_instance_methods.include? method return klass, method, params end |
#dont_persist_params(&block) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rad/router/core/router.rb', line 142 def dont_persist_params &block if block before = safe_workspace.persist_params? begin safe_workspace.delete :persist_params block.call ensure safe_workspace[:persist_params] = true if before end else safe_workspace.delete :persist_params end end |
#encode(klass, method, params = {}) ⇒ Object
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 104 105 106 107 |
# File 'lib/rad/router/core/router.rb', line 79 def encode klass, method, params = {} klass.must_be.defined method.must_be.a Symbol params = params.clone # method, params = method, params.to_openobject format = params.delete(:format) raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.method_defined? method raise "Invalid route! You try to call protected method '#{method}' for '#{klass}'!" unless klass.method_defined? method inject_persistent_params! params result = nil routes.each do |name, route| result = route.encode klass, method, params break if result end raise "no route for '#{klass}.#{method}'!" unless result path, params = result path.must_be.defined params.must_be.defined path = format_processor.add_format path, format if format_processor and format return path, params end |
#encode_method(route_method) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rad/router/core/router.rb', line 109 def encode_method route_method # method = method route_method.must_be.present result = nil routes.each do |name, route| result = route.respond_to :encode_method, route_method break if result end raise "no route for '#{route_method}' route method!" unless result klass, method = result klass.must_be.defined method.must_be.defined return klass, method end |
#persist_params(&block) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/rad/router/core/router.rb', line 128 def persist_params &block if block before = safe_workspace.persist_params? begin safe_workspace[:persist_params] = true block.call ensure safe_workspace.delete :persist_params unless before end else safe_workspace[:persist_params] = true end end |
#persist_params? ⇒ Boolean
156 157 158 |
# File 'lib/rad/router/core/router.rb', line 156 def persist_params? safe_workspace.persist_params? end |
#persistent_params ⇒ Object
160 |
# File 'lib/rad/router/core/router.rb', line 160 def persistent_params; @persistent_params ||= [] end |
#skip(path_regexp = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/rad/router/core/router.rb', line 23 def skip path_regexp=nil @skip ||= [] if path_regexp path_regexp.must_be.a Regexp @skip << path_regexp unless @skip.include? path_regexp else @skip end end |
#url_for(*args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rad/router/core/router.rb', line 33 def url_for *args if args.first.is_a?(Class) args.size.must_be.in 2..3 klass, method, = args url_for_class(klass, method, ( || {})) else first = args.first.to_s if first !~ /^\// args.size.must_be.in 1..2 method, = args url_for_class(nil, method, ( || {})) else args.size.must_be.in 1..2 path, = args url_for_path(path, ( || {})) end end end |
#url_for_class(klass, method, params) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/rad/router/core/router.rb', line 199 def url_for_class klass, method, params klass ||= current_class # params = params.to_openobject # special params format = params[:format] path, params = encode klass, method, params url = url_for_path path, params url.marks.format = format.to_s if format url end |
#url_for_path(path, params) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rad/router/core/router.rb', line 162 def url_for_path path, params # params = params.to_openobject # special params url = if params.include? :url_root (params.delete(:url_root) || "") + path else config.url_root! + path end host, port = params.delete(:host), params.delete(:port) format = params.delete(:format) # json params = {json: params.to_json} if params.delete :as_json #and !params.empty? # format if format params[:format] = format url.marks.format = format.to_s end # Delete 'nil' parameters to_delete = [] params.each{|k, v| to_delete << k if v.nil?} to_delete.each{|k| params.delete k} # build url delimiter = path.include?('?') ? '&' : '?' url << "#{delimiter}#{params.to_query}" unless params.empty? if host.blank? url else %{http://#{host}#{":#{port}" unless port.blank?}#{url}} end end |