Class: Gempilot::GemConstant
- Inherits:
-
Data
- Object
- Data
- Gempilot::GemConstant
- 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
-
#gem_module ⇒ Object
readonly
Returns the value of attribute gem_module.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#require_path ⇒ Object
readonly
Returns the value of attribute require_path.
Instance Method Summary collapse
-
#lib_path ⇒ Object
Path to the constant’s source file, e.g.
-
#name ⇒ Object
The final class or module name.
-
#namespaces ⇒ Object
Namespace segments preceding the final constant name.
-
#qualified ⇒ Object
The fully-qualified constant, rooted at the gem module.
-
#test_path(framework) ⇒ Object
Path to the constant’s test file for
framework(:rspecor:minitest); correct for multi-segment (hyphenated) gem modules.
Instance Attribute Details
#gem_module ⇒ Object (readonly)
Returns the value of attribute gem_module
13 14 15 |
# File 'lib/gempilot/gem_constant.rb', line 13 def gem_module @gem_module end |
#input ⇒ Object (readonly)
Returns the value of attribute input
13 14 15 |
# File 'lib/gempilot/gem_constant.rb', line 13 def input @input end |
#require_path ⇒ Object (readonly)
Returns the value of attribute require_path
13 14 15 |
# File 'lib/gempilot/gem_constant.rb', line 13 def require_path @require_path end |
Instance Method Details
#lib_path ⇒ Object
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 |
#name ⇒ Object
The final class or module name.
27 28 29 |
# File 'lib/gempilot/gem_constant.rb', line 27 def name parts.last end |
#namespaces ⇒ Object
Namespace segments preceding the final constant name.
22 23 24 |
# File 'lib/gempilot/gem_constant.rb', line 22 def namespaces parts[0...-1] end |
#qualified ⇒ Object
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 |