Module: Archsight::Editor::ContentHasher

Defined in:
lib/archsight/editor/content_hasher.rb

Overview

ContentHasher generates SHA256 hashes for optimistic locking

Class Method Summary collapse

Class Method Details

.hash(content) ⇒ String

Generate a hash of YAML content for comparison Normalizes line endings before hashing to ensure consistency across platforms

Parameters:

  • content (String)

    YAML content

Returns:

  • (String)

    16-character hex hash



15
16
17
18
# File 'lib/archsight/editor/content_hasher.rb', line 15

def hash(content)
  normalized = content.gsub("\r\n", "\n").gsub("\r", "\n")
  Digest::SHA256.hexdigest(normalized)[0, 16]
end

.validate(path:, start_line:, expected_hash:) ⇒ Hash?

Validate that content hasn’t changed since expected_hash was computed

Parameters:

  • path (String)

    File path

  • start_line (Integer)

    Line number where document starts

  • expected_hash (String, nil)

    Expected content hash

Returns:

  • (Hash, nil)

    Error hash with :conflict and :error keys, or nil if valid



25
26
27
28
29
30
31
32
33
34
# File 'lib/archsight/editor/content_hasher.rb', line 25

def validate(path:, start_line:, expected_hash:)
  return nil unless expected_hash

  current_content = FileWriter.read_document(path: path, start_line: start_line)
  current_hash = hash(current_content)

  return nil if current_hash == expected_hash

  { conflict: true, error: "Conflict: The resource has been modified. Please reload the page and try again." }
end