Class: Lutaml::Ea::Diagram::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/lutaml/ea/diagram/extractor.rb

Overview

API for extracting and rendering UML diagrams from repositories

This class provides programmatic access to diagram extraction and rendering functionality. It follows API-first architecture, with all business logic in this class rather than in CLI layer.

Examples:

Extract single diagram

extractor = DiagramExtractor.new
result = extractor.extract_one(
  "model.lur",
  "diagram001",
  output: "diagram.svg"
)

List all diagrams

diagrams = extractor.list_diagrams("model.lur")
diagrams.each { |d| puts "#{d[:name]} (#{d[:type]})" }

Batch extraction

results = extractor.extract_batch(
  "model.lur",
  ["dia1", "dia2", "dia3"],
  output_dir: "diagrams/"
)

Constant Summary collapse

DEFAULT_OPTIONS =

Default rendering options

{
  format: "svg",
  padding: 20,
  background_color: "#ffffff",
  grid_visible: false,
  interactive: false,
  config_path: nil,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Extractor

Initialize extractor with options

Parameters:

  • options (Hash) (defaults to: {})

    Extraction options

Options Hash (options):

  • :padding (Integer)

    Padding around diagram

  • :background_color (String)

    Background color

  • :grid_visible (Boolean)

    Show grid lines

  • :interactive (Boolean)

    Enable interactivity

  • :config_path (String)

    Path to diagram config



53
54
55
# File 'lib/lutaml/ea/diagram/extractor.rb', line 53

def initialize(options = {})
  @options = resolve_options(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/lutaml/ea/diagram/extractor.rb', line 43

def options
  @options
end

Instance Method Details

#extract_batch(lur_path, diagram_ids, opts = {}) ⇒ Hash

Extract multiple diagrams in batch

Parameters:

  • lur_path (String)

    Path to LUR repository file

  • diagram_ids (Array<String>)

    Array of diagram IDs

  • opts (Hash) (defaults to: {})

    Additional options

Options Hash (opts):

  • :output_dir (String)

    Output directory

Returns:

  • (Hash)

    Result with :success, :results, :summary



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/lutaml/ea/diagram/extractor.rb', line 142

def extract_batch(lur_path, diagram_ids, opts = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  merged_opts = @options.merge(opts)
  output_dir = merged_opts[:output_dir] || "."

  # Create output directory if needed
  FileUtils.mkdir_p(output_dir)

  results = diagram_ids.map do |diagram_id|
    output_path = File.join(output_dir,
                            "#{sanitize_filename(diagram_id)}.svg")
    extract_one(lur_path, diagram_id,
                merged_opts.merge(output: output_path))
  end

  successful = results.count { |r| r[:success] }
  failed = results.count { |r| !r[:success] }

  {
    success: failed.zero?,
    results: results,
    summary: {
      total: diagram_ids.size,
      successful: successful,
      failed: failed,
    },
  }
rescue StandardError => e
  {
    success: false,
    message: "Batch extraction failed: #{e.message}",
    error: e,
  }
end

#extract_one(lur_path, diagram_id, opts = {}) ⇒ Hash

Extract and render a single diagram

Parameters:

  • lur_path (String)

    Path to LUR repository file

  • diagram_id (String)

    Diagram ID or name

  • opts (Hash) (defaults to: {})

    Additional options

Options Hash (opts):

  • :output (String)

    Output file path

Returns:

  • (Hash)

    Result with :success, :path, :diagram, :message



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lutaml/ea/diagram/extractor.rb', line 64

def extract_one(lur_path, diagram_id, opts = {}) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  merged_opts = @options.merge(opts)

  # Load repository
  repository = load_repository(lur_path)

  # Find diagram
  diagram = find_diagram(repository, diagram_id)
  unless diagram
    return {
      success: false,
      message: "Diagram not found: #{diagram_id}",
      available: repository.all_diagrams.map(&:name),
    }
  end

  # Convert to rendering format
  diagram_data = convert_to_rendering_format(diagram, repository)

  # Render
  svg_content = render_diagram(diagram_data, merged_opts)

  # Determine output path
  output_path = merged_opts[:output]

  # Write file if output path specified
  File.write(output_path, svg_content) if output_path

  result = {
    success: true,
    diagram: diagram_info(diagram),
    format: merged_opts[:format],
    message: "Diagram rendered successfully",
  }

  # Include path if file was written
  result[:path] = output_path if output_path

  # Include SVG content if no output file (for testing)
  result[:svg_content] = svg_content unless output_path

  result
rescue StandardError => e
  {
    success: false,
    message: "Failed to extract diagram: #{e.message}",
    error: e,
  }
end

#list_diagrams(lur_path) ⇒ Hash

List all diagrams in repository

Parameters:

  • lur_path (String)

    Path to LUR repository file

Returns:

  • (Hash)

    Result with :success, :diagrams, :count, :message



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lutaml/ea/diagram/extractor.rb', line 118

def list_diagrams(lur_path) # rubocop:disable Metrics/MethodLength
  repository = load_repository(lur_path)
  diagrams = repository.all_diagrams

  {
    success: true,
    count: diagrams.size,
    diagrams: diagrams.map { |d| diagram_info(d) },
  }
rescue StandardError => e
  {
    success: false,
    message: "Failed to list diagrams: #{e.message}",
    error: e,
  }
end