Class: Sarif::Graph

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

Overview

A network of nodes and directed edges that describes some aspect of the structure of the code (for example, a call graph).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description: nil, nodes: [], edges: [], properties: nil) ⇒ Graph

Returns a new instance of Graph.



8
9
10
11
12
13
# File 'lib/sarif/graph.rb', line 8

def initialize(description: nil, nodes: [], edges: [], properties: nil)
  @description = description
  @nodes = nodes
  @edges = edges
  @properties = properties
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/sarif/graph.rb', line 6

def description
  @description
end

#edgesObject

Returns the value of attribute edges.



6
7
8
# File 'lib/sarif/graph.rb', line 6

def edges
  @edges
end

#nodesObject

Returns the value of attribute nodes.



6
7
8
# File 'lib/sarif/graph.rb', line 6

def nodes
  @nodes
end

#propertiesObject

Returns the value of attribute properties.



6
7
8
# File 'lib/sarif/graph.rb', line 6

def properties
  @properties
end

Class Method Details

.from_hash(h) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/sarif/graph.rb', line 28

def self.from_hash(h)
  return nil if h.nil?
  new(
    description: Message.from_hash(h["description"]),
    nodes: h["nodes"]&.map { |v| Node.from_hash(v) } || [],
    edges: h["edges"]&.map { |v| Edge.from_hash(v) } || [],
    properties: h["properties"]
  )
end

Instance Method Details

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



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

def ==(other)
  return false unless other.is_a?(Graph)
  @description == other.description && @nodes == other.nodes && @edges == other.edges && @properties == other.properties
end

#hashObject



45
46
47
# File 'lib/sarif/graph.rb', line 45

def hash
  [@description, @nodes, @edges, @properties].hash
end

#to_hObject



15
16
17
18
19
20
21
22
# File 'lib/sarif/graph.rb', line 15

def to_h
  h = {}
  h["description"] = @description&.to_h unless @description.nil?
  h["nodes"] = @nodes&.map(&:to_h) if @nodes&.any?
  h["edges"] = @edges&.map(&:to_h) if @edges&.any?
  h["properties"] = @properties unless @properties.nil?
  h
end

#to_json(pretty: false) ⇒ Object



24
25
26
# File 'lib/sarif/graph.rb', line 24

def to_json(pretty: false)
  pretty ? JSON.pretty_generate(to_h) : JSON.generate(to_h)
end