Class: Inspec::Requirement

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/dependencies/requirement.rb

Overview

Inspec::Requirement represents a given profile dependency, where appropriate we delegate to Inspec::Profile directly.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version_constraints, cache, cwd, opts) ⇒ Requirement

Returns a new instance of Requirement.



33
34
35
36
37
38
39
40
# File 'lib/inspec/dependencies/requirement.rb', line 33

def initialize(name, version_constraints, cache, cwd, opts)
  @name = name
  @required_version = Gem::Requirement.new(Array(version_constraints))
  @cache = cache
  @backend = opts[:backend]
  @opts = opts
  @cwd = cwd
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



32
33
34
# File 'lib/inspec/dependencies/requirement.rb', line 32

def cwd
  @cwd
end

#optsObject (readonly)

Returns the value of attribute opts.



32
33
34
# File 'lib/inspec/dependencies/requirement.rb', line 32

def opts
  @opts
end

#required_versionObject (readonly)

Returns the value of attribute required_version.



32
33
34
# File 'lib/inspec/dependencies/requirement.rb', line 32

def required_version
  @required_version
end

Class Method Details

.from_lock_entry(entry, cwd, cache, backend, opts = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inspec/dependencies/requirement.rb', line 17

def self.from_lock_entry(entry, cwd, cache, backend, opts = {})
  req = new(entry[:name],
            entry[:version_constraints],
            cache,
            cwd,
            entry[:resolved_source].merge(backend: backend).merge(opts))

  locked_deps = []
  Array(entry[:dependencies]).each do |dep_entry|
    locked_deps << Inspec::Requirement.from_lock_entry(dep_entry, cwd, cache, backend, opts)
  end
  req.lock_deps(locked_deps)
  req
end

.from_metadata(dep, cache, opts) ⇒ Object



12
13
14
15
# File 'lib/inspec/dependencies/requirement.rb', line 12

def self.(dep, cache, opts)
  raise 'Cannot load empty dependency.' if dep.nil? || dep.empty?
  new(dep[:name], dep[:version], cache, opts[:cwd], opts.merge(dep))
end

Instance Method Details

#dependenciesObject

load dependencies of the dependency



89
90
91
92
93
# File 'lib/inspec/dependencies/requirement.rb', line 89

def dependencies
  @dependencies ||= profile..dependencies.map do |r|
    Inspec::Requirement.(r, @cache, cwd: @cwd, backend: @backend)
  end
end

#fetcherObject



84
85
86
# File 'lib/inspec/dependencies/requirement.rb', line 84

def fetcher
  @fetcher ||= Inspec::CachedFetcher.new(opts, @cache)
end

#gem_depObject



58
59
60
# File 'lib/inspec/dependencies/requirement.rb', line 58

def gem_dep
  @gem_dep ||= Gem::Dependency.new(profile.name, required_version, :runtime)
end

#lock_deps(dep_array) ⇒ Object



80
81
82
# File 'lib/inspec/dependencies/requirement.rb', line 80

def lock_deps(dep_array)
  @dependencies = dep_array
end

#nameObject

A dependency can be renamed in inspec.yml/inspec.lock. Prefer the name the user gave this dependency over the profile name.



46
47
48
# File 'lib/inspec/dependencies/requirement.rb', line 46

def name
  @name || profile.name
end

#profileObject

load the profile for the requirement



100
101
102
103
104
105
106
107
108
# File 'lib/inspec/dependencies/requirement.rb', line 100

def profile
  return @profile if ! @profile.nil?
  opts = @opts.dup
  opts[:backend] = @backend
  if !@dependencies.nil?
    opts[:dependencies] = Inspec::DependencySet.from_array(@dependencies, @cwd, @cache, @backend)
  end
  @profile = Inspec::Profile.for_fetcher(fetcher, opts)
end

#resolved_sourceObject



62
63
64
# File 'lib/inspec/dependencies/requirement.rb', line 62

def resolved_source
  @resolved_source ||= fetcher.resolved_source
end

#source_satisfies_spec?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/inspec/dependencies/requirement.rb', line 54

def source_satisfies_spec?
  gem_dep.match?(profile.name, profile.version)
end

#source_versionObject



50
51
52
# File 'lib/inspec/dependencies/requirement.rb', line 50

def source_version
  profile.version
end

#to_hashObject



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/inspec/dependencies/requirement.rb', line 66

def to_hash
  h = {
    'name' => name,
    'resolved_source' => resolved_source,
    'version_constraints' => required_version.to_s,
  }

  if !dependencies.empty?
    h['dependencies'] = dependencies.map(&:to_hash)
  end

  h
end

#to_sObject



95
96
97
# File 'lib/inspec/dependencies/requirement.rb', line 95

def to_s
  name
end