Class: GemCP::LocalRepository
- Inherits:
-
Object
- Object
- GemCP::LocalRepository
- Defined in:
- lib/gemcp/local_repository.rb
Overview
Reads gem metadata from installed gemspecs on the local system. All methods return nil or empty arrays when nothing matches — never raises for missing gems.
Instance Method Summary collapse
-
#gem(name) ⇒ Hash?
Find the latest installed version of a gem by name.
-
#search(query) ⇒ Array<Hash>
Search installed gems by name or summary substring match.
-
#version(name, number, platform: nil) ⇒ Hash?
Find a specific installed version, optionally filtered by platform.
-
#versions(name) ⇒ Array<Hash>
List all installed versions of a gem, newest first.
Instance Method Details
#gem(name) ⇒ Hash?
Find the latest installed version of a gem by name.
14 15 16 17 |
# File 'lib/gemcp/local_repository.rb', line 14 def gem(name) spec = Gem::Specification.find_all_by_name(name).max_by(&:version) spec && serialize(spec, source: "local") end |
#search(query) ⇒ Array<Hash>
Search installed gems by name or summary substring match.
50 51 52 53 54 55 56 57 |
# File 'lib/gemcp/local_repository.rb', line 50 def search(query) needle = query.downcase Gem::Specification.each .select { |spec| spec.name.downcase.include?(needle) || spec.summary.to_s.downcase.include?(needle) } .group_by(&:name) .values .map { |specs| serialize(specs.max_by(&:version), source: "local") } end |
#version(name, number, platform: nil) ⇒ Hash?
Find a specific installed version, optionally filtered by platform.
25 26 27 28 29 30 31 32 33 |
# File 'lib/gemcp/local_repository.rb', line 25 def version(name, number, platform: nil) specs = Gem::Specification.find_all_by_name(name, Gem::Requirement.new("=#{number}")) spec = if platform specs.find { |candidate| candidate.platform.to_s == platform } else specs.first end spec && serialize(spec, source: "local") end |
#versions(name) ⇒ Array<Hash>
List all installed versions of a gem, newest first.
39 40 41 42 43 44 |
# File 'lib/gemcp/local_repository.rb', line 39 def versions(name) Gem::Specification.find_all_by_name(name) .sort_by(&:version) .reverse .map { |spec| serialize_version(spec) } end |