Class: Dependabot::FileParsers::Base::DependencySet

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/file_parsers/base/dependency_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependencies = []) ⇒ DependencySet

Returns a new instance of DependencySet.



11
12
13
14
15
16
17
18
# File 'lib/dependabot/file_parsers/base/dependency_set.rb', line 11

def initialize(dependencies = [])
  unless dependencies.is_a?(Array) &&
         dependencies.all? { |dep| dep.is_a?(Dependency) }
    raise ArgumentError, "must be an array of Dependency objects"
  end

  @dependencies = dependencies
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



20
21
22
# File 'lib/dependabot/file_parsers/base/dependency_set.rb', line 20

def dependencies
  @dependencies
end

Instance Method Details

#+(other) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dependabot/file_parsers/base/dependency_set.rb', line 41

def +(other)
  unless other.is_a?(DependencySet)
    raise ArgumentError, "must be a DependencySet"
  end

  other.dependencies.each { |dep| self << dep }
  self
end

#<<(dep) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dependabot/file_parsers/base/dependency_set.rb', line 22

def <<(dep)
  unless dep.is_a?(Dependency)
    raise ArgumentError, "must be a Dependency object"
  end

  existing_dependency = dependencies.find { |d| d.name == dep.name }

  return self if existing_dependency&.to_h == dep.to_h

  if existing_dependency
    dependencies[dependencies.index(existing_dependency)] =
      combined_dependency(existing_dependency, dep)
  else
    dependencies << dep
  end

  self
end