Module: Rigor::ModuleGraph::CLI::EdgeFilters

Included in:
ClassDiagramCmd, Cycles, Render, StatsCmd, View
Defined in:
lib/rigor/module_graph/cli.rb

Overview

Shared filter options reused by dot / mermaid / cycles / view.

Constant Summary collapse

VALID_KINDS =
Rigor::ModuleGraph::EDGE_KINDS
VALID_CONFIDENCES =
Rigor::ModuleGraph::EDGE_CONFIDENCES
VALID_DIRECTIONS =
Reachability::VALID_DIRECTIONS
VALID_EDGE_SCOPES =
Reachability::VALID_EDGE_SCOPES

Instance Method Summary collapse

Instance Method Details

#add_filter_options(opts, state) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rigor/module_graph/cli.rb', line 117

def add_filter_options(opts, state)
  opts.on("--kind KINDS", Array,
          "Only render the listed edge kinds (#{VALID_KINDS.join(",")})") do |list|
    state[:kinds] = validate!(list, VALID_KINDS, "kind")
  end
  opts.on("--confidence LEVELS", Array,
          "Only render the listed confidence levels (#{VALID_CONFIDENCES.join(",")})") do |list|
    state[:confidences] = validate!(list, VALID_CONFIDENCES, "confidence")
  end
  opts.on("--from NAMES", Array,
          "Restrict the graph to nodes reachable from NAMES (comma-separated)") do |names|
    state[:from] = names
  end
  opts.on("--depth N", Integer,
          "Maximum hops from --from roots (default: unlimited)") do |n|
    state[:depth] = n
  end
  opts.on("--direction DIR", VALID_DIRECTIONS.map(&:to_s),
          "Direction to follow from --from roots (#{VALID_DIRECTIONS.join(", ")}; default: both)") do |dir|
    state[:direction] = dir.to_sym
  end
  opts.on("--edge-scope SCOPE", VALID_EDGE_SCOPES.map(&:to_s),
          "Edges to keep when --from is set: cluster keeps every edge whose " \
          "endpoints both fall in the reachable node set; walk keeps only " \
          "the edges the BFS actually traverses " \
          "(#{VALID_EDGE_SCOPES.join("|")}; default: cluster)") do |scope|
    state[:edge_scope] = scope.to_sym
  end
end

#apply_filters(edges, kinds:, confidences:, from: nil, depth: nil, direction: :both, edge_scope: :cluster) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rigor/module_graph/cli.rb', line 105

def apply_filters(edges, kinds:, confidences:, from: nil, depth: nil,
                  direction: :both, edge_scope: :cluster)
  edges = edges.select { |e| kinds.include?(e.kind) } if kinds
  edges = edges.select { |e| confidences.include?(e.confidence) } if confidences
  if from && !from.empty?
    edges = Reachability.filter(
      edges, roots: from, depth: depth, direction: direction, edge_scope: edge_scope
    )
  end
  edges
end

#validate!(list, allowed, label) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/rigor/module_graph/cli.rb', line 147

def validate!(list, allowed, label)
  unknown = list - allowed
  unless unknown.empty?
    raise OptionParser::InvalidArgument,
          "unknown #{label}(s): #{unknown.join(",")}. Allowed: #{allowed.join(",")}"
  end
  list
end