Module: ClassSpecHelper::Naming

Included in:
ClassSpecHelper
Defined in:
lib/class_spec_helper/naming.rb

Instance Method Summary collapse

Instance Method Details

#class_name_from_name(name) ⇒ Object

provided with a fully qualified class name, return the class name

For example:

namespace_from_name "Foo::Bar::Baz" will return the string "Baz"
namespace_from_name "Foo" will return the string "Foo"


29
30
31
# File 'lib/class_spec_helper/naming.rb', line 29

def class_name_from_name name
  name.split("::").last
end

#namespace_from_name(fully_qualified_class_name) ⇒ Object

provided with a fully qualified class name, return the namespace

For example:

namespace_from_name "Foo::Bar::Baz" will return the constant Foo
namespace_from_name "Foo" will return the constant Object


10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/class_spec_helper/naming.rb', line 10

def namespace_from_name fully_qualified_class_name
  namespace_parts = fully_qualified_class_name.split("::")[0..-2]
  namespace_name = namespace_parts.join("::")

  # if the model was namespaced, then return the coresponding constant
  # if there is no namespace, then use `Object` which is the top most
  # namespace in ruby
  if namespace_name == ""
    Object
  else
    Object.const_get namespace_name
  end
end