Class: Fractor::QueuePersister::JSONPersister
- Defined in:
- lib/fractor/queue_persister.rb
Overview
JSON file persister. Stores queue state as a JSON array.
Instance Method Summary collapse
-
#clear ⇒ Boolean
Clear the JSON file.
-
#initialize(path, pretty: true) ⇒ JSONPersister
constructor
Initialize a JSON persister.
-
#load ⇒ Array<Hash>?
Load work items from JSON file.
-
#save(items) ⇒ Boolean
Save work items to JSON file.
Constructor Details
#initialize(path, pretty: true) ⇒ JSONPersister
Initialize a JSON persister.
65 66 67 68 |
# File 'lib/fractor/queue_persister.rb', line 65 def initialize(path, pretty: true) @path = path @pretty = pretty end |
Instance Method Details
#clear ⇒ Boolean
Clear the JSON file.
105 106 107 108 109 110 111 |
# File 'lib/fractor/queue_persister.rb', line 105 def clear File.delete(@path) if File.exist?(@path) true rescue StandardError => e warn "Failed to clear #{@path}: #{e.}" false end |
#load ⇒ Array<Hash>?
Load work items from JSON file.
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/fractor/queue_persister.rb', line 90 def load return nil unless File.exist?(@path) json = File.read(@path) return nil if json.strip.empty? JSON.parse(json) rescue StandardError => e warn "Failed to load from #{@path}: #{e.}" nil end |
#save(items) ⇒ Boolean
Save work items to JSON file.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/fractor/queue_persister.rb', line 74 def save(items) ensure_directory_exists serialized = items.map { |work| serialize_work(work) } json = @pretty ? JSON.pretty_generate(serialized) : JSON.generate(serialized) File.write(@path, json) true rescue StandardError => e warn "Failed to save to #{@path}: #{e.}" false end |