Class: Gempilot::GemConstant

Inherits:
Data
  • Object
show all
Defined in:
lib/gempilot/gem_constant.rb

Overview

A class or module constant resolved within a gem’s namespace.

Wraps raw user input (a bare suffix like Services::Auth or a fully-qualified MyGem::Services::Auth) together with the gem’s module and require path, and derives the qualified constant, file paths, and namespace pieces from a single parse.

Every constant is rooted at the gem’s module: bare input is prefixed with it, while input already starting with the gem’s root segment is left as is. Rooting is matched on the first segment only, so an extension gem whose module is My::Gem accepts any My::... constant.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gem_moduleObject (readonly)

Returns the value of attribute gem_module

Returns:

  • (Object)

    the current value of gem_module



13
14
15
# File 'lib/gempilot/gem_constant.rb', line 13

def gem_module
  @gem_module
end

#inputObject (readonly)

Returns the value of attribute input

Returns:

  • (Object)

    the current value of input



13
14
15
# File 'lib/gempilot/gem_constant.rb', line 13

def input
  @input
end

#require_pathObject (readonly)

Returns the value of attribute require_path

Returns:

  • (Object)

    the current value of require_path



13
14
15
# File 'lib/gempilot/gem_constant.rb', line 13

def require_path
  @require_path
end

Instance Method Details

#lib_pathObject

Path to the constant’s source file, e.g. lib/my_gem/services/auth.rb.



32
33
34
# File 'lib/gempilot/gem_constant.rb', line 32

def lib_path
  "#{File.join("lib", *path_segments)}.rb"
end

#nameObject

The final class or module name.



27
28
29
# File 'lib/gempilot/gem_constant.rb', line 27

def name
  parts.last
end

#namespacesObject

Namespace segments preceding the final constant name.



22
23
24
# File 'lib/gempilot/gem_constant.rb', line 22

def namespaces
  parts[0...-1]
end

#qualifiedObject

The fully-qualified constant, rooted at the gem module.



17
18
19
# File 'lib/gempilot/gem_constant.rb', line 17

def qualified
  input.start_with?("#{root_segment}::") ? input : "#{gem_module}::#{input}"
end

#test_path(framework) ⇒ Object

Path to the constant’s test file for framework (:rspec or :minitest); correct for multi-segment (hyphenated) gem modules.



38
39
40
41
42
43
44
45
# File 'lib/gempilot/gem_constant.rb', line 38

def test_path(framework)
  rest = path_segments.drop(require_path.split("/").length)
  if framework == :rspec
    "#{File.join("spec", require_path, *rest)}_spec.rb"
  else
    "#{File.join("test", require_path, *rest)}_test.rb"
  end
end