Class: K8::ActionMapping

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

Constant Summary collapse

EMPTY_ARRAY =

:nodoc:

[].freeze

Instance Method Summary collapse

Constructor Details

#initialize(urlpath_mapping, default_patterns: DEFAULT_PATTERNS, urlpath_cache_size: 0, enable_urlpath_param_range: true) ⇒ ActionMapping

Returns a new instance of ActionMapping.



1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
# File 'lib/keight.rb', line 1198

def initialize(urlpath_mapping, default_patterns: DEFAULT_PATTERNS, urlpath_cache_size: 0,
                                enable_urlpath_param_range: true)
  @default_patterns   = default_patterns || DefaultPatterns.new
  #; [!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.
  compile(urlpath_mapping)
end

Instance Method Details

#compile(urlpath_mapping) ⇒ Object



1210
1211
1212
1213
1214
1215
1216
1217
1218
# File 'lib/keight.rb', line 1210

def compile(urlpath_mapping)
  #; [!6f3vl] compiles urlpath mapping.
  @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)
  rexp_str = _compile_array(urlpath_mapping, '', '')
  @urlpath_rexp       = Regexp.compile("\\A#{rexp_str}\\z")
  return self
end

#eachObject



1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
# File 'lib/keight.rb', line 1271

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

#lookup(req_urlpath) ⇒ Object



1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
# File 'lib/keight.rb', line 1222

def lookup(req_urlpath)
  #; [!j34yh] finds from fixed urlpaths at first.
  if (tuple = @fixed_endpoints[req_urlpath])
    _, action_class, action_methods = tuple
    pnames = pvalues = EMPTY_ARRAY
    return action_class, action_methods, pnames, pvalues
  end
  #; [!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 and methods, parameter names and values.
  result = [action_class, action_methods, pnames, 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