Class: Rage::Router::Util

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

Constant Summary collapse

@@names_map =
{}

Class Method Summary collapse

Class Method Details

.path_to_class(str) ⇒ Object

converts controller name in a path form into a class api/v1/users => Api::V1::UsersController



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rage/router/util.rb', line 5

def path_to_class(str)
  str = str.capitalize
  str.gsub!(/([\/_])([a-zA-Z0-9]+)/) do
    if $1 == "/"
      "::#{$2.capitalize}"
    else
      $2.capitalize
    end
  end

  klass = "#{str}Controller"
  if Object.const_defined?(klass)
    Object.const_get(klass)
  else
    raise Rage::Errors::RouterError, "Routing error: could not find the #{klass} class"
  end
end

.path_to_name(str) ⇒ Object

converts controller name in a path form into a string representation of a class api/v1/users => "Api::V1::UsersController"



27
28
29
30
31
# File 'lib/rage/router/util.rb', line 27

def path_to_name(str)
  @@names_map[str] || begin
    @@names_map[str] = path_to_class(str).name
  end
end