Class: Solve::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/solve/graph.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGraph

Returns a new instance of Graph.



43
44
45
# File 'lib/solve/graph.rb', line 43

def initialize
  @artifacts = Hash.new
end

Class Method Details

.artifact_key(name, version) ⇒ Symbol

Create a key representing an artifact for an instance of Graph

Parameters:

  • name (#to_s)
  • version (#to_s)

Returns:

  • (Symbol)


28
29
30
# File 'lib/solve/graph.rb', line 28

def artifact_key(name, version)
  "#{name}-#{version}".to_sym
end

.dependency_key(name, constraint) ⇒ Symbol

Create a key representing an dependency for an instance of Graph

Parameters:

  • name (#to_s)
  • constraint (#to_s)

Returns:

  • (Symbol)


38
39
40
# File 'lib/solve/graph.rb', line 38

def dependency_key(name, constraint)
  "#{name}-#{constraint}".to_sym
end

.key_for(object) ⇒ Symbol

Create a key for a graph from an instance of an Artifact or Dependency

Parameters:

Returns:

  • (Symbol)

Raises:

  • (ArgumentError)

    if an instance of an object of an unknown type is given



11
12
13
14
15
16
17
18
19
20
# File 'lib/solve/graph.rb', line 11

def key_for(object)
  case object
  when Solve::Artifact
    artifact_key(object.name, object.version)
  when Solve::Dependency
    dependency_key(object.name, object.constraint)
  else
    raise ArgumentError, "Could not generate graph key for Class: #{object.class}"
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/solve/graph.rb', line 139

def ==(other)
  return false unless other.is_a?(self.class)

  self_artifacts = self.artifacts
  other_artifacts = other.artifacts

  self_dependencies = self_artifacts.inject([]) do |list, artifact|
    list << artifact.dependencies
  end.flatten

  other_dependencies = other_artifacts.inject([]) do |list, artifact|
    list << artifact.dependencies
  end.flatten

  self_artifacts.size == other_artifacts.size &&
    self_dependencies.size == other_dependencies.size &&
    self_artifacts.all? { |artifact| other_artifacts.include?(artifact) } &&
    self_dependencies.all? { |dependency| other_dependencies.include?(dependency) }
end

#add_artifact(artifact) ⇒ Solve::Artifact

Add a Solve::Artifact to the collection of artifacts and return the added Solve::Artifact. No change will be made if the artifact is already a member of the collection.

Parameters:

Returns:



98
99
100
101
102
103
104
# File 'lib/solve/graph.rb', line 98

def add_artifact(artifact)
  unless has_artifact?(artifact.name, artifact.version)
    @artifacts[self.class.key_for(artifact)] = artifact
  end

  get_artifact(artifact.name, artifact.version)
end

#artifacts(name, version) ⇒ Solve::Artifact #artifactsArray<Solve::Artifact>

Overloads:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/solve/graph.rb', line 59

def artifacts(*args)
  if args.empty?
    return artifact_collection
  end
  unless args.length == 2
    raise ArgumentError, "Unexpected number of arguments. You gave: #{args.length}. Expected: 0 or 2."
  end

  name, version = args

  if name.nil? || version.nil?
    raise ArgumentError, "A name and version must be specified. You gave: #{args}."
  end

  artifact = Artifact.new(self, name, version)
  add_artifact(artifact)
end

#get_artifact(name, version) ⇒ Solve::Artifact?

Retrieve the artifact from the graph with the matching name and version

Parameters:

Returns:



112
113
114
# File 'lib/solve/graph.rb', line 112

def get_artifact(name, version)
  @artifacts.fetch(self.class.artifact_key(name, version.to_s), nil)
end

#has_artifact?(name, version) ⇒ Boolean

Check if an artifact with a matching name and version is a member of this instance of graph

Parameters:

Returns:

  • (Boolean)


132
133
134
# File 'lib/solve/graph.rb', line 132

def has_artifact?(name, version)
  !get_artifact(name, version).nil?
end

#remove_artifact(artifact) ⇒ Object

Remove the given instance of artifact from the graph

Parameters:



119
120
121
122
123
# File 'lib/solve/graph.rb', line 119

def remove_artifact(artifact)
  if has_artifact?(artifact.name, artifact.version)
    @artifacts.delete(self.class.key_for(artifact))
  end
end

#versions(name, constraint = ">= 0.0.0") ⇒ Array<Solve::Artifact>

Return all the artifacts from the collection of artifacts with the given name.

Parameters:

  • name (String)

Returns:



83
84
85
86
87
88
89
# File 'lib/solve/graph.rb', line 83

def versions(name, constraint = ">= 0.0.0")
  constraint = constraint.is_a?(Constraint) ? constraint : Constraint.new(constraint)

  artifacts.select do |art|
    art.name == name && constraint.satisfies?(art.version)
  end
end