Class: Solargraph::Metagem

Inherits:
Object
  • Object
show all
Includes:
Equality
Defined in:
lib/solargraph/metagem.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equality

#==, #eql?, #freeze, #hash

Constructor Details

#initialize(name:, full_path:, spec_file:, source:, version:, require_paths:, dependencies:) ⇒ Metagem

Returns a new instance of Metagem.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/solargraph/metagem.rb', line 31

def initialize name:, full_path:, spec_file:, source:, version:, require_paths:, dependencies:
  @name = name
  @full_path = full_path
  @spec_file = spec_file
  @source = source
  @version = version
  @require_paths = require_paths
  @dependencies = dependencies
  # @todo Possible change to cache names to make them more specific to sources
  # @cache_name = "#{File.basename(full_path)}__#{source.gsub(/[^a-z0-9\-_]/i, '_')}" unless source.end_with?('Path')
  # @todo Better path source detection
  @cache_name = File.basename(full_path) unless source.start_with?('source at') || source.to_s.end_with?('::Path')
end

Instance Attribute Details

#cache_name ⇒ String (readonly)

Returns:

  • (String)


29
30
31
# File 'lib/solargraph/metagem.rb', line 29

def cache_name
  @cache_name
end

#dependencies ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


26
27
28
# File 'lib/solargraph/metagem.rb', line 26

def dependencies
  @dependencies
end

#full_path ⇒ String (readonly) Also known as: full_gem_path

Returns:

  • (String)


11
12
13
# File 'lib/solargraph/metagem.rb', line 11

def full_path
  @full_path
end

#name ⇒ String (readonly)

Returns:

  • (String)


8
9
10
# File 'lib/solargraph/metagem.rb', line 8

def name
  @name
end

#require_paths ⇒ Array<String> (readonly)

Returns:

  • (Array<String>)


23
24
25
# File 'lib/solargraph/metagem.rb', line 23

def require_paths
  @require_paths
end

#source ⇒ String (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/solargraph/metagem.rb', line 17

def source
  @source
end

#spec_file ⇒ String (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/solargraph/metagem.rb', line 14

def spec_file
  @spec_file
end

#version ⇒ String (readonly)

Returns:

  • (String)


20
21
22
# File 'lib/solargraph/metagem.rb', line 20

def version
  @version
end

Class Method Details

.from_specification(gem) ⇒ Metagem

Parameters:

  • gem (Gem::Specification)

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/solargraph/metagem.rb', line 70

def self.from_specification gem
  Metagem.new(name: gem.name,
              full_path: gem.full_gem_path,
              spec_file: gem.spec_file,
              source: gem.source.to_s,
              version: gem.version,
              require_paths: gem.require_paths,
              dependencies: gem.runtime_dependencies.map(&:name))
end

Instance Method Details

#cacheable? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/solargraph/metagem.rb', line 45

def cacheable?
  !!@cache_name
end

#equality_fields ⇒ Array

Returns:

  • (Array)


62
63
64
65
66
# File 'lib/solargraph/metagem.rb', line 62

def equality_fields
  # A version arrives as a String from a bundle definition and as a
  # Gem::Version from a specification, so compare its string form.
  [name, version.to_s, full_path, source]
end

#require?(path) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/solargraph/metagem.rb', line 49

def require? path
  require_paths.any? do |req|
    File.file?(File.join(full_path, req, "#{path}.rb"))
  end
end

#to_specification ⇒ Object



55
56
57
# File 'lib/solargraph/metagem.rb', line 55

def to_specification
  Gem::Specification.load(spec_file)
end