Class: Plutonium::Wizard::LazyPersisted
- Inherits:
-
Object
- Object
- Plutonium::Wizard::LazyPersisted
- Defined in:
- lib/plutonium/wizard/lazy_persisted.rb
Overview
Lazy view over a wizard run's persisted records (§2.2 / §4.5).
The stored state only holds GIDs ({ "step_key" => [gid, ...] }); resolving
them back into live records costs a GlobalID::Locator.locate per GID. Most
requests (a GET render, a back, any step whose condition:/render never
reads persisted) don't need those records at all, so we resolve LAZILY:
persisted[:key]returns the memoized live records when the key was SET this request (records created via thepersistmacro inon_submit) — no locate.- Otherwise it locates the GIDs stored under that key ONCE, memoizes the
result, and returns it. A request that never reads
persistedissues zero locate queries.
gid_source is the { "step_key" => [gids] } hash the runner injects from
the stored state. Writes (persisted[k] = records) memoize live records
directly and shadow any stored GIDs for that key.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Live records for a step key.
-
#[]=(key, records) ⇒ Object
Memoize live records for a key directly (the runner's post-
on_submitset, or an author assigning intopersisted). -
#initialize(gid_source = {}) ⇒ LazyPersisted
constructor
A new instance of LazyPersisted.
-
#key?(key) ⇒ Boolean
(also: #has_key?)
Whether this key has records available — either set this request or stored as GIDs.
-
#keys ⇒ Object
All known step keys (memoized + stored), as symbols, without locating.
-
#to_h ⇒ Object
Resolve every known key to its located records (forces locates).
Constructor Details
#initialize(gid_source = {}) ⇒ LazyPersisted
Returns a new instance of LazyPersisted.
23 24 25 26 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 23 def initialize(gid_source = {}) @gid_source = gid_source || {} @memo = {} end |
Instance Method Details
#[](key) ⇒ Object
Live records for a step key. Memoized records (set this request) are returned as-is; otherwise the key's stored GIDs are located once.
30 31 32 33 34 35 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 30 def [](key) key = key.to_sym return @memo[key] if @memo.key?(key) @memo[key] = locate(@gid_source[key.to_s] || @gid_source[key]) end |
#[]=(key, records) ⇒ Object
Memoize live records for a key directly (the runner's post-on_submit
set, or an author assigning into persisted). No locate on later reads.
39 40 41 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 39 def []=(key, records) @memo[key.to_sym] = Array(records) end |
#key?(key) ⇒ Boolean Also known as: has_key?
Whether this key has records available — either set this request or stored as GIDs. Does NOT trigger a locate.
45 46 47 48 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 45 def key?(key) key = key.to_sym @memo.key?(key) || @gid_source.key?(key.to_s) || @gid_source.key?(key) end |
#keys ⇒ Object
All known step keys (memoized + stored), as symbols, without locating.
58 59 60 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 58 def keys (@memo.keys + @gid_source.keys.map(&:to_sym)).uniq end |
#to_h ⇒ Object
Resolve every known key to its located records (forces locates). Used where the full map is genuinely needed.
53 54 55 |
# File 'lib/plutonium/wizard/lazy_persisted.rb', line 53 def to_h keys.each_with_object({}) { |key, acc| acc[key] = self[key] } end |