Class: Ace::Bundle::Molecules::BundleFileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/bundle/molecules/bundle_file_writer.rb

Overview

BundleFileWriter handles writing bundle to files with caching and chunking Configuration values (cache_dir, max_lines) are loaded from Ace::Bundle.config following ADR-022 pattern.

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: nil, max_lines: nil) ⇒ BundleFileWriter

Returns a new instance of BundleFileWriter.



15
16
17
18
19
# File 'lib/ace/bundle/molecules/bundle_file_writer.rb', line 15

def initialize(cache_dir: nil, max_lines: nil)
  @cache_dir = cache_dir || Ace::Bundle.cache_dir
  @max_lines = max_lines || Ace::Bundle.max_lines
  @chunker = BundleChunker.new(@max_lines)
end

Instance Method Details

#write(content, path, options = {}) ⇒ Object

Write single file



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ace/bundle/molecules/bundle_file_writer.rb', line 42

def write(content, path, options = {})
  # Ensure directory exists
  dir = File.dirname(path)
  unless File.directory?(dir)
    FileUtils.mkdir_p(dir)
    # Validate that directory was created successfully
    unless File.directory?(dir)
      return {
        success: false,
        error: "Failed to create directory: #{dir}",
        path: path
      }
    end
  end

  # Write file
  File.write(path, content)

  # Calculate statistics
  lines = content.lines.size
  size = content.bytesize

  {
    success: true,
    path: path,
    lines: lines,
    size: size,
    size_formatted: format_bytes(size)
  }
rescue => e
  {
    success: false,
    error: e.message,
    path: path
  }
end

#write_with_chunking(bundle, output_path, options = {}) ⇒ Object

Write bundle with optional chunking



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ace/bundle/molecules/bundle_file_writer.rb', line 22

def write_with_chunking(bundle, output_path, options = {})
  # Check if we should organize by sections
  if options[:organize_by_sections] && bundle.respond_to?(:has_sections?) && bundle.has_sections?
    write_sections_organized(bundle, output_path, options)
  else
    content = format_content(bundle, options[:format])

    # Determine actual output path
    path = resolve_output_path(output_path)

    # Check if chunking is needed
    if @chunker.needs_chunking?(content)
      write_chunked_content(content, path, options)
    else
      write_single_file(content, path, options)
    end
  end
end