Module: Textus::Builder::IdempotentWrite
- Defined in:
- lib/textus/builder/pipeline.rb
Overview
Replaces the freshly-stamped timestamp inside ‘new_bytes` with the timestamp pulled from `old_bytes` (same format). Returns the rewritten bytes, or nil if either side lacks a parseable timestamp.
Class Method Summary collapse
- .extract_timestamp(bytes, format) ⇒ Object
- .rewrite_with_prior_timestamp(new_bytes:, old_bytes:, format:) ⇒ Object
Class Method Details
.extract_timestamp(bytes, format) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/textus/builder/pipeline.rb', line 35 def self.(bytes, format) case format when "markdown" parsed = Entry.for_format("markdown").parse(bytes) parsed.dig("_meta", "generated", "at") when "json", "yaml" parsed = Entry.for_format(format).parse(bytes) parsed.dig("_meta", "generated_at") else # rubocop:disable Style/EmptyElse nil end rescue Textus::BadFrontmatter nil end |
.rewrite_with_prior_timestamp(new_bytes:, old_bytes:, format:) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/textus/builder/pipeline.rb', line 26 def self.(new_bytes:, old_bytes:, format:) prior = (old_bytes, format) fresh = (new_bytes, format) return nil unless prior && fresh return new_bytes if prior == fresh new_bytes.sub(fresh, prior) end |