Class: RailsAiBridge::Serializers::RuleFileWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai_bridge/serializers/rule_file_writer.rb

Overview

Writes generated Markdown files to disk with a diff guard: a file is written only when its generated content differs from the content on disk, so unchanged files keep their mtime and stay out of VCS diffs.

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ RuleFileWriter

Returns a new instance of RuleFileWriter.

Parameters:

  • dir (String) —

    target directory, created recursively if missing



12
13
14
# File 'lib/rails_ai_bridge/serializers/rule_file_writer.rb', line 12

def initialize(dir)
  @dir = dir
end

Instance Method Details

#call(files) ⇒ Hash{Symbol => Array<String>}

Writes each entry whose content differs from the file on disk, creating the target directory first. Entries with nil content are skipped without creating a file.

Parameters:

  • files (Hash{String => String, nil}) —

    filenames relative to the target directory mapped to generated content

Returns:

  • (Hash{Symbol => Array<String>}) —

    :written and :skipped arrays of absolute file paths



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_ai_bridge/serializers/rule_file_writer.rb', line 24

def call(files)
  FileUtils.mkdir_p(@dir)

  written = []
  skipped = []
  files.each do |filename, content|
    next unless content

    filepath = File.join(@dir, filename)
    if File.exist?(filepath) && File.read(filepath) == content
      skipped << filepath
    else
      File.write(filepath, content)
      written << filepath
    end
  end
  { written: written, skipped: skipped }
end