Class: Textus::Store::Envelope::Writer
- Inherits:
-
Object
- Object
- Textus::Store::Envelope::Writer
- Defined in:
- lib/textus/store/envelope/writer.rb
Overview
Owns the write pipeline (validate, serialize, etag-check, write, audit). Talks to ports (FileStore, Schemas, AuditLog, Manifest) and an Reader for the existing-uid lookup.
Invariant: every public method’s final action is @audit_log.append(…).
No permission check, no event firing — those belong to the caller (Write::Put / ::Delete / ::Mv).
Defined Under Namespace
Classes: Payload
Class Method Summary collapse
Instance Method Summary collapse
-
#delete(key, mentry: nil, if_etag: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#initialize(file_store:, manifest:, schemas:, audit_log:, call:, reader:, geometry:) ⇒ Writer
constructor
rubocop:disable Metrics/ParameterLists.
- #move(from_key:, to_key:, new_mentry:, if_etag: nil) ⇒ Object
- #put(key, mentry:, payload:, if_etag: nil) ⇒ Object
Constructor Details
#initialize(file_store:, manifest:, schemas:, audit_log:, call:, reader:, geometry:) ⇒ Writer
rubocop:disable Metrics/ParameterLists
26 27 28 29 30 31 32 33 34 |
# File 'lib/textus/store/envelope/writer.rb', line 26 def initialize(file_store:, manifest:, schemas:, audit_log:, call:, reader:, geometry:) # rubocop:disable Metrics/ParameterLists @file_store = file_store @manifest = manifest @schemas = schemas @audit_log = audit_log @call = call @reader = reader @geometry = geometry end |
Class Method Details
.from(container:, call:) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/textus/store/envelope/writer.rb', line 17 def self.from(container:, call:) new( file_store: container.file_store, manifest: container.manifest, schemas: container.schemas, audit_log: container.audit_log, call: call, reader: Reader.from(container: container), geometry: container.geometry ) end |
Instance Method Details
#delete(key, mentry: nil, if_etag: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/textus/store/envelope/writer.rb', line 57 def delete(key, mentry: nil, if_etag: nil) # rubocop:disable Lint/UnusedMethodArgument # `mentry:` is accepted for symmetry with `put` / `move` and to # leave room for future format-specific delete hooks; no field # on it is needed today. path = @manifest.resolver.resolve(key).path raise UnknownKey.new(key, suggestions: @manifest.resolver.suggestions_for(key)) unless @file_store.exists?(path) etag_before = @file_store.etag(path) raise EtagMismatch.new(key, if_etag, etag_before) if if_etag && if_etag != etag_before @file_store.delete(path) prune_empty_parents(path) @audit_log.append( role: @call.role, verb: "key_delete", key: key, etag_before: etag_before, etag_after: nil, extras: @call.correlation_id ? { "correlation_id" => @call.correlation_id } : nil ) end |
#move(from_key:, to_key:, new_mentry:, if_etag: nil) ⇒ Object
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 |
# File 'lib/textus/store/envelope/writer.rb', line 76 def move(from_key:, to_key:, new_mentry:, if_etag: nil) from_path = @manifest.resolver.resolve(from_key).path to_path = @manifest.resolver.resolve(to_key).path raise UnknownKey.new(from_key, suggestions: @manifest.resolver.suggestions_for(from_key)) unless @file_store.exists?(from_path) etag_before = @file_store.etag(from_path) raise EtagMismatch.new(from_key, if_etag, etag_before) if if_etag && if_etag != etag_before FileUtils.mkdir_p(File.dirname(to_path)) FileUtils.mv(from_path, to_path) prune_empty_parents(from_path) basename = to_key.split(".").last Format.for(new_mentry.format).rewrite_name(to_path, basename) etag_after = Value::Etag.for_file(to_path) envelope = @reader.read(to_key) extras = { "from_key" => from_key, "to_key" => to_key, "from_path" => from_path, "to_path" => to_path, "uid" => envelope.uid } extras["correlation_id"] = @call.correlation_id if @call.correlation_id @audit_log.append( role: @call.role, verb: "key_mv", key: to_key, etag_before: etag_before, etag_after: etag_after, extras: extras ) envelope end |
#put(key, mentry:, payload:, if_etag: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/textus/store/envelope/writer.rb', line 36 def put(key, mentry:, payload:, if_etag: nil) path = resolve_path(key) = payload. || {} content = payload.content existing_env = @reader.read(key) = existing_env ? existing_env. : {} , content = Textus::Meta.inject_all(, content, , format: mentry.format) bytes, , eff_body, eff_content = serialize_entry(mentry, path, , payload, content) enforce_name_match!(path, , mentry.format) validate_schema(mentry, , eff_content) Textus::Format::Yaml.validate_raw_entry!( { "_meta" => , "content" => eff_content }, mentry.lane, ) etag_before = check_etag!(path, key, if_etag) write_bytes(path, bytes) envelope = build_envelope(key, mentry, path, , eff_body, eff_content, bytes) audit_put(key, etag_before, envelope.etag) envelope end |