Class: GemWhy::JSONOutputter

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_why/json_outputter.rb

Overview

Handles JSON output for all display modes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, tree_builder) ⇒ JSONOutputter

Returns a new instance of JSONOutputter.



10
11
12
13
# File 'lib/gem_why/json_outputter.rb', line 10

def initialize(command, tree_builder)
  @command = command
  @tree_builder = tree_builder
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



8
9
10
# File 'lib/gem_why/json_outputter.rb', line 8

def command
  @command
end

#tree_builderObject (readonly)

Returns the value of attribute tree_builder.



8
9
10
# File 'lib/gem_why/json_outputter.rb', line 8

def tree_builder
  @tree_builder
end

Instance Method Details

#output_deep(gem_name, chains) ⇒ void

This method returns an undefined value.

Outputs deep dependency chains as JSON

Parameters:

  • gem_name (String)

    the target gem name

  • chains (Array<Array<Hash>>)

    the dependency chains



32
33
34
35
36
37
38
39
40
41
# File 'lib/gem_why/json_outputter.rb', line 32

def output_deep(gem_name, chains)
  chains_by_root = chains.group_by { |chain| chain.first[:name] }
  target = gem_name
  mode = "deep"
  chains_data = chains.map { |chain| chain.map { |node| node.slice(:name, :version, :dependency, :requirement) } }
  root_gems = chains_by_root.keys.size
  total_chains = chains.size
  output = { target:, mode:, chains: chains_data, root_gems:, total_chains: }
  say JSON.pretty_generate(output)
end

#output_direct(gem_name, dependents) ⇒ void

This method returns an undefined value.

Outputs direct dependencies as JSON

Parameters:

  • gem_name (String)

    the target gem name

  • dependents (Array<Dependent>)

    the dependent gems



19
20
21
22
23
24
25
26
# File 'lib/gem_why/json_outputter.rb', line 19

def output_direct(gem_name, dependents)
  target = gem_name
  mode = "direct"
  total = dependents.size
  dependents_data = dependents.map(&:to_h)
  output = { target:, mode:, dependents: dependents_data, total: }
  say JSON.pretty_generate(output)
end

#output_tree(gem_name, chains) ⇒ void

This method returns an undefined value.

Outputs dependency tree as JSON

Parameters:

  • gem_name (String)

    the target gem name

  • chains (Array<Array<Hash>>)

    the dependency chains



47
48
49
50
51
52
53
54
55
# File 'lib/gem_why/json_outputter.rb', line 47

def output_tree(gem_name, chains)
  chains_by_root = chains.group_by { |chain| chain.first[:name] }
  target = gem_name
  mode = "tree"
  roots = build_json_roots(chains_by_root)
  total_roots = chains_by_root.keys.size
  output = { target:, mode:, roots:, total_roots: }
  say JSON.pretty_generate(output)
end