Module: Archsight::Editor::ContentHasher
- Defined in:
- lib/archsight/editor/content_hasher.rb
Overview
ContentHasher generates SHA256 hashes for optimistic locking
Class Method Summary collapse
-
.hash(content) ⇒ String
Generate a hash of YAML content for comparison Normalizes line endings before hashing to ensure consistency across platforms.
-
.validate(path:, start_line:, expected_hash:) ⇒ Hash?
Validate that content hasn’t changed since expected_hash was computed.
Class Method Details
.hash(content) ⇒ String
Generate a hash of YAML content for comparison Normalizes line endings before hashing to ensure consistency across platforms
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
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 |