Class: K8::ActionMapping
- Inherits:
-
Object
- Object
- K8::ActionMapping
- Defined in:
- lib/keight.rb
Instance Attribute Summary collapse
-
#urlpath_rexp ⇒ Object
readonly
Returns the value of attribute urlpath_rexp.
Instance Method Summary collapse
- #each ⇒ Object
- #find(req_urlpath) ⇒ Object
-
#initialize(urlpath_mapping, urlpath_cache_size: 0, enable_urlpath_param_range: true) ⇒ ActionMapping
constructor
A new instance of ActionMapping.
Constructor Details
#initialize(urlpath_mapping, urlpath_cache_size: 0, enable_urlpath_param_range: true) ⇒ ActionMapping
Returns a new instance of ActionMapping.
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 |
# File 'lib/keight.rb', line 1068 def initialize(urlpath_mapping, urlpath_cache_size: 0, enable_urlpath_param_range: true) #; [!34o67] keyword arg 'enable_urlpath_param_range' controls to generate range object or not. @enable_urlpath_param_range = enable_urlpath_param_range #; [!buj0d] prepares LRU cache if cache size specified. @urlpath_cache_size = urlpath_cache_size @urlpath_lru_cache = urlpath_cache_size > 0 ? {} : nil #; [!wsz8g] compiles urlpath mapping passed. @fixed_endpoints = {} # urlpath patterns which have no urlpath params @variable_endpoints = [] # urlpath patterns which have any ulrpath param @all_endpoints = [] # all urlpath patterns (fixed + variable) @urlpath_rexp = build(urlpath_mapping) end |
Instance Attribute Details
#urlpath_rexp ⇒ Object (readonly)
Returns the value of attribute urlpath_rexp.
1082 1083 1084 |
# File 'lib/keight.rb', line 1082 def urlpath_rexp @urlpath_rexp end |
Instance Method Details
#each ⇒ Object
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
# File 'lib/keight.rb', line 1332 def each #; [!2gwru] returns Enumerator if block is not provided. return to_enum(:each) unless block_given? #; [!7ynne] yields each urlpath pattern, action class and action methods. @all_endpoints.each do |tuple| urlpath_pat, action_class, action_methods, _ = tuple yield urlpath_pat, action_class, action_methods end self end |
#find(req_urlpath) ⇒ Object
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 |
# File 'lib/keight.rb', line 1286 def find(req_urlpath) #; [!j34yh] finds from fixed urlpaths at first. tuple = @fixed_endpoints[req_urlpath] return tuple[1..-1] if tuple # ex: [BooksAction, {:GET=>:do_index}, []] #; [!uqwr7] uses LRU as cache algorithm. cache = @urlpath_lru_cache if cache && (result = cache.delete(req_urlpath)) cache[req_urlpath] = result # delete & append to simulate LRU return result end #; [!sos5i] returns nil when request path not matched to urlpath patterns. m = @urlpath_rexp.match(req_urlpath) return nil unless m #; [!95q61] finds from variable urlpath patterns when not found in fixed ones. index = m.captures.find_index('') tuple = @variable_endpoints[index] _, action_class, action_methods, urlpath_rexp, pnames, procs, range = tuple #; [!1k1k5] converts urlpath param values by converter procs. if range str = req_urlpath[range] pvalues = [procs[0] ? procs[0].call(str) : str] else strs = urlpath_rexp.match(req_urlpath).captures pvalues = \ case procs.length when 1; [procs[0] ? procs[0].call(strs[0]) : strs[0]] when 2; [procs[0] ? procs[0].call(strs[0]) : strs[0], procs[1] ? procs[1].call(strs[1]) : strs[1]] when 3; [procs[0] ? procs[0].call(strs[0]) : strs[0], procs[1] ? procs[1].call(strs[1]) : strs[1], procs[2] ? procs[2].call(strs[2]) : strs[2]] else ; procs.zip(strs).map {|pr, v| pr ? pr.call(v) : v } end # ex: ["123"] -> [123] end #; [!jyxlm] returns action class, action methods and urlpath param args. result = [action_class, action_methods, pvalues] #; [!uqwr7] stores result into cache if cache is enabled. if cache cache[req_urlpath] = result #; [!3ps5g] deletes item from cache when cache size over limit. cache.shift() if cache.length > @urlpath_cache_size end # return result end |