Class: Spandx::Core::Dependency

Inherits:
Object
  • Object
show all
Defined in:
lib/spandx/core/dependency.rb

Constant Summary collapse

PACKAGE_MANAGERS =
{
  Spandx::Dotnet::Parsers::Csproj => :nuget,
  Spandx::Dotnet::Parsers::PackagesConfig => :nuget,
  Spandx::Dotnet::Parsers::Sln => :nuget,
  Spandx::Java::Parsers::Maven => :maven,
  Spandx::Js::Parsers::Npm => :npm,
  Spandx::Js::Parsers::Yarn => :yarn,
  Spandx::Php::Parsers::Composer => :composer,
  Spandx::Python::Parsers::PipfileLock => :pypi,
  Spandx::Ruby::Parsers::GemfileLock => :rubygems,
  Spandx::Os::Parsers::Apk => :apk,
  Spandx::Os::Parsers::Dpkg => :dpkg,
  Spandx::Terraform::Parsers::LockFile => :terraform,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, version:, path:, meta: {}) ⇒ Dependency

Returns a new instance of Dependency.



22
23
24
25
26
27
28
# File 'lib/spandx/core/dependency.rb', line 22

def initialize(name:, version:, path:, meta: {})
  @path = Pathname.new(path).realpath
  @name = name || @path.basename.to_s
  @version = version || @path.mtime.to_i.to_s
  @licenses = []
  @meta = meta
end

Instance Attribute Details

#licensesObject (readonly)

Returns the value of attribute licenses.



20
21
22
# File 'lib/spandx/core/dependency.rb', line 20

def licenses
  @licenses
end

#metaObject (readonly)

Returns the value of attribute meta.



20
21
22
# File 'lib/spandx/core/dependency.rb', line 20

def meta
  @meta
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/spandx/core/dependency.rb', line 20

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



20
21
22
# File 'lib/spandx/core/dependency.rb', line 20

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version.



20
21
22
# File 'lib/spandx/core/dependency.rb', line 20

def version
  @version
end

Instance Method Details

#<=>(other) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/spandx/core/dependency.rb', line 34

def <=>(other)
  return 1 if other.nil?

  score = (name <=> other.name)
  score = score.zero? ? (version <=> other&.version) : score
  score.zero? ? (path.to_s <=> other&.path.to_s) : score
end

#==(other) ⇒ Object



46
47
48
# File 'lib/spandx/core/dependency.rb', line 46

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/spandx/core/dependency.rb', line 50

def eql?(other)
  to_s == other.to_s
end

#hashObject



42
43
44
# File 'lib/spandx/core/dependency.rb', line 42

def hash
  to_s.hash
end

#inspectObject



58
59
60
# File 'lib/spandx/core/dependency.rb', line 58

def inspect
  "#<#{self.class} name=#{name} version=#{version} path=#{relative_path}>"
end

#package_managerObject



30
31
32
# File 'lib/spandx/core/dependency.rb', line 30

def package_manager
  PACKAGE_MANAGERS[Parser.for(path).class]
end

#to_aObject



62
63
64
# File 'lib/spandx/core/dependency.rb', line 62

def to_a
  [name, version, license_expression, relative_path.to_s]
end

#to_hObject



66
67
68
69
70
71
72
73
# File 'lib/spandx/core/dependency.rb', line 66

def to_h
  {
    name: name,
    version: version,
    licenses: license_expression,
    path: relative_path.to_s
  }
end

#to_sObject



54
55
56
# File 'lib/spandx/core/dependency.rb', line 54

def to_s
  @to_s ||= [name, version, path].compact.join(' ')
end