<commit_before>App = require('scripts/application')
AppConfig = require('scripts/config')

class RoutesHelper
  @isInited: false

  @init: ->
    return if @isInited

    @isInited = true

    for moduleName, module of App.submodules
      continue unless module.router?

      for pattern, routeName of module.router.appRoutes
        addRoute(moduleName, routeName, pattern)

  @rootPath: -> AppConfig.rootPath

  rootCheck = (path) =>
    rootPath = @rootPath()
    if path.indexOf(rootPath) then "#{rootPath}#{path}" else path

  addRoute = (moduleName, routeName, pattern) =>
    keys = pattern.match(/\:\w+/g)
    methodName = "#{moduleName.toLowerCase()}#{routeName.charAt(0).toUpperCase()}#{routeName.substr(1).toLowerCase()}Path"

    @[methodName] = (params...) ->
      return rootCheck(pattern) unless keys

      if keys.length isnt params.length
        throw new Error("incorrect params count (#{params.length} for #{keys.length})")

      pattern = pattern.replace(/\:\w+/, param) for param in params

      rootCheck(pattern)

module.exports = RoutesHelper<commit_msg>routes helper refactoring<commit_after>App = require('scripts/application')
AppConfig = require('scripts/config')

class RoutesHelper
  @isInited: false

  @init: ->
    return if @isInited

    for moduleName, module of App.submodules when module.router?
      for pattern, routeName of module.router.appRoutes
        addRoute(moduleName, routeName, pattern)

    @isInited = true

  @rootPath: -> AppConfig.rootPath

  checkRoot = (path) =>
    rootPath = @rootPath()
    if path.indexOf(rootPath) then "#{rootPath}#{path}" else path

  addRoute = (moduleName, routeName, pattern) =>
    keys = pattern.match(/\:\w+/g)
    methodName = "#{moduleName.toLowerCase()}#{routeName.charAt(0).toUpperCase()}#{routeName.substr(1).toLowerCase()}Path"

    @[methodName] = (params...) ->
      return checkRoot(pattern) unless keys

      if keys.length isnt params.length
        throw new Error("incorrect params count (#{params.length} for #{keys.length})")

      pattern = pattern.replace(/\:\w+/, param) for param in params

      checkRoot(pattern)

module.exports = RoutesHelper
{'fraction_non_alphanumeric': 0.1367837338262477, 'fraction_numerical': 0.0018484288354898336, 'mean_word_length': 3.676025917926566, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4990325', 'n_tokens_mistral': 663, 'n_tokens_neox': 660, 'n_words': 156}