Class: FiberAudit::Runtime::JSONL::Writer
- Inherits:
-
Object
- Object
- FiberAudit::Runtime::JSONL::Writer
- Defined in:
- lib/fiber_audit/runtime/jsonl/writer.rb
Instance Attribute Summary collapse
-
#bytes_written ⇒ Object
readonly
Returns the value of attribute bytes_written.
-
#max_record_bytes ⇒ Object
readonly
Returns the value of attribute max_record_bytes.
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #close ⇒ Object
- #closed? ⇒ Boolean
- #failed? ⇒ Boolean
-
#initialize(io:, max_record_bytes:, owns_io: false) ⇒ Writer
constructor
A new instance of Writer.
- #prepare(record) ⇒ Object
- #write(record) ⇒ Object
- #write_line(line) ⇒ Object
Constructor Details
#initialize(io:, max_record_bytes:, owns_io: false) ⇒ Writer
Returns a new instance of Writer.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 25 def initialize(io:, max_record_bytes:, owns_io: false) raise RuntimeContractError, 'io must respond to write' unless io.respond_to?(:write) unless max_record_bytes.is_a?(Integer) && max_record_bytes.positive? raise RuntimeContractError, 'max_record_bytes must be a positive Integer' end raise RuntimeContractError, 'owns_io must be a Boolean' unless [true, false].include?(owns_io) @io = io @max_record_bytes = max_record_bytes @owns_io = owns_io @bytes_written = 0 @state = :active end |
Instance Attribute Details
#bytes_written ⇒ Object (readonly)
Returns the value of attribute bytes_written.
11 12 13 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 11 def bytes_written @bytes_written end |
#max_record_bytes ⇒ Object (readonly)
Returns the value of attribute max_record_bytes.
11 12 13 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 11 def max_record_bytes @max_record_bytes end |
Class Method Details
.open(path:, max_record_bytes:) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 13 def self.open(path:, max_record_bytes:) safe_path = Validation.string(path, 'runtime output path', max_bytes: 4_096) # The returned Writer deliberately owns this descriptor until #close. io = File.open(safe_path, File::WRONLY | File::CREAT | File::EXCL, 0o600) # rubocop:disable Style/FileOpen io.binmode io.sync = true new(io: io, max_record_bytes: max_record_bytes, owns_io: true) rescue StandardError io&.close raise end |
Instance Method Details
#active? ⇒ Boolean
62 63 64 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 62 def active? @state == :active end |
#close ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 74 def close return if closed? completed = false begin @io.close if @owns_io && @io.respond_to?(:close) && !@io.closed? completed = true ensure @state = :failed unless completed end @state = :closed unless failed? nil end |
#closed? ⇒ Boolean
70 71 72 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 70 def closed? @state == :closed end |
#failed? ⇒ Boolean
66 67 68 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 66 def failed? @state == :failed end |
#prepare(record) ⇒ Object
39 40 41 42 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 39 def prepare(record) ensure_active! Schema.dump(record, max_record_bytes: max_record_bytes) end |
#write(record) ⇒ Object
44 45 46 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 44 def write(record) write_line(prepare(record)) end |
#write_line(line) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fiber_audit/runtime/jsonl/writer.rb', line 48 def write_line(line) ensure_active! validate_line!(line) completed = false begin write_all(line) @io.flush if @io.respond_to?(:flush) completed = true ensure @state = :failed unless completed end line.bytesize end |