From f1516df659c4163ae9940cd7939f3f885b13c5ec Mon Sep 17 00:00:00 2001 From: shinokaro Date: Sun, 23 Jun 2024 10:36:15 +0900 Subject: [PATCH] Improve GemSpecQueryable module - Renamed the parameter of the find_gem_path method to a name that indicates it is a file path. - Renamed find_gemspec_path method to find_spec_file and changed its parameter name to clearly indicate it accepts a file path. Updated the method to handle path strings as input. - Implemented the find_spec method which retrieves the corresponding Gem::Specification for a given path. - Updated Ocran.find_gem_files method to utilize find_spec for better functionality and clarity. --- bin/ocran | 3 +-- lib/ocran/gem_spec_queryable.rb | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/bin/ocran b/bin/ocran index 0facb28..a625185 100644 --- a/bin/ocran +++ b/bin/ocran @@ -425,8 +425,7 @@ EOF features_from_gems << feature next end - spec_path = GemSpecQueryable.find_gemspec_path(feature) - if spec_path && (spec = Gem::Specification.load(spec_path)) + if (spec = GemSpecQueryable.find_spec(feature)) gems[spec.name] ||= spec features_from_gems << feature else diff --git a/lib/ocran/gem_spec_queryable.rb b/lib/ocran/gem_spec_queryable.rb index 203f9f5..de59e7d 100644 --- a/lib/ocran/gem_spec_queryable.rb +++ b/lib/ocran/gem_spec_queryable.rb @@ -19,15 +19,27 @@ module GemSpecQueryable GEM_NON_FILE_RE = /(#{GEM_EXTRA_RE}|#{GEM_SCRIPT_RE})/ class << self - def find_gem_path(feature) + # find_gem_path method searches for the path of the gem containing the + # specified path. The 'path' argument is a file or directory path. + # It checks each gem's installation path in Gem.path to see if the + # specified path is a subpath. Returns the gem's path if found, or + # nil if not found. + def find_gem_path(path) return unless defined?(Gem) - Gem.path.find { |path| Pathname(feature).subpath?(path) } + Gem.path.find { |gem_path| Pathname(path).subpath?(gem_path) } end - def find_gemspec_path(feature) + # find_spec_file method searches for the path of the gemspec file of + # the gem containing the specified path. The 'path' argument is a file + # or directory path. It searches within the "gems" directory in each + # directory listed in Gem.path to check if the specified path is a + # subpath. If the gemspec file exists, it returns its path; otherwise, + # it returns nil. + def find_spec_file(path) return unless defined?(Gem) + feature = Pathname(path) Gem.path.each do |gem_path| gems_dir = File.join(gem_path, "gems") next unless feature.subpath?(gems_dir) @@ -38,6 +50,19 @@ def find_gemspec_path(feature) end nil end + + # find_spec method searches and returns a Gem::Specification object + # based on the specified path. Internally, it uses find_spec_file to + # obtain the path to the gemspec file, and if that file exists, it + # calls Gem::Specification.load to load the gem's specifications. + # Returns the loaded Gem::Specification object, or nil if the gemspec + # file does not exist. + def find_spec(path) + return unless defined?(Gem) + + spec_file = find_spec_file(path) + spec_file && Gem::Specification.load(spec_file) + end end def gem_root = Pathname(gem_dir)