Class: HttpResource::Simulation::Store
- Inherits:
-
Object
- Object
- HttpResource::Simulation::Store
- Defined in:
- lib/http_resource/simulation/store.rb
Overview
Per-Backend in-memory record store. Collection-agnostic: ANY name auto-vivifies an empty collection on first access. Records are Hashes with STRING keys — they stand in for parsed JSON, so the value objects and resource proxies above read them exactly as they would a live response body.
Instance Method Summary collapse
-
#[](collection) ⇒ Object
store — the collection's Array (auto-vivified, live reference: handlers mutate it in place).
-
#deep_stringify(value) ⇒ Object
Public because handlers need the same JSON-shaping for records they build from request payloads.
-
#initialize ⇒ Store
constructor
A new instance of Store.
-
#next_id(collection) ⇒ Object
Allocate the next Integer id for a collection.
-
#reset! ⇒ Object
Clears the collections IN PLACE, so previously-obtained collection references (see #[]) stay live across a reset.
-
#seed(collections) ⇒ Object
Append records under ANY collection names, assigning "id" via next_id when absent: seed(contacts: [...], "invoices" => [...]).
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
11 12 13 14 |
# File 'lib/http_resource/simulation/store.rb', line 11 def initialize @collections = Hash.new { |hash, key| hash[key] = [] } @sequences = Hash.new(0) end |
Instance Method Details
#[](collection) ⇒ Object
store — the collection's Array (auto-vivified, live reference: handlers mutate it in place).
18 19 20 |
# File 'lib/http_resource/simulation/store.rb', line 18 def [](collection) @collections[collection.to_sym] end |
#deep_stringify(value) ⇒ Object
Public because handlers need the same JSON-shaping for records they build from request payloads.
54 55 56 57 58 59 60 |
# File 'lib/http_resource/simulation/store.rb', line 54 def deep_stringify(value) case value when Hash then value.to_h { |key, val| [key.to_s, deep_stringify(val)] } when Array then value.map { deep_stringify(_1) } else value end end |
#next_id(collection) ⇒ Object
Allocate the next Integer id for a collection. MUTATING — every call consumes an id. Never call it from a test assertion; read the records' "id" values instead.
25 26 27 |
# File 'lib/http_resource/simulation/store.rb', line 25 def next_id(collection) @sequences[collection.to_sym] += 1 end |
#reset! ⇒ Object
Clears the collections IN PLACE, so previously-obtained collection references (see #[]) stay live across a reset.
46 47 48 49 50 |
# File 'lib/http_resource/simulation/store.rb', line 46 def reset! @collections.each_value(&:clear) @sequences.clear nil end |
#seed(collections) ⇒ Object
Append records under ANY collection names, assigning "id" via next_id when absent: seed(contacts: [...], "invoices" => [...]). Symbol- or string-keyed records; all keys are normalized (deeply) to strings.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/http_resource/simulation/store.rb', line 32 def seed(collections) collections.each do |name, records| unless records.is_a?(Array) raise ArgumentError, "records for #{name.inspect} must be an Array of Hashes, got #{records.class}" end records.each { append(name, _1) } end nil end |