Class: Julewire::Core::Fields::FieldStack
- Inherits:
-
Object
- Object
- Julewire::Core::Fields::FieldStack
- Defined in:
- lib/julewire/core/fields/field_stack.rb
Overview
Immutable layers keep snapshots stable while each stack tracks only its current head and versioned read caches.
Instance Method Summary collapse
- #add(fields = nil, owned: false, **keyword_fields) ⇒ Object
- #delete(path) ⇒ Object
- #fork ⇒ Object
-
#initialize(fields = {}, delete_paths: false, source: nil) ⇒ FieldStack
constructor
A new instance of FieldStack.
- #snapshot ⇒ Object
- #value_for(key, default:) ⇒ Object
- #with(fields = nil, owned: false, **keyword_fields) ⇒ Object
- #without(path) ⇒ Object
Constructor Details
#initialize(fields = {}, delete_paths: false, source: nil) ⇒ FieldStack
Returns a new instance of FieldStack.
156 157 158 159 160 161 162 163 164 |
# File 'lib/julewire/core/fields/field_stack.rb', line 156 def initialize(fields = {}, delete_paths: false, source: nil) @source = source @delete_paths_enabled = delete_paths @version = 0 @snapshot_version = nil @snapshot = nil @value_cache = nil add(fields) if fields.is_a?(Hash) && !fields.empty? end |
Instance Method Details
#add(fields = nil, owned: false, **keyword_fields) ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/julewire/core/fields/field_stack.rb', line 194 def add(fields = nil, owned: false, **keyword_fields) fields = field_input(fields, keyword_fields, owned: owned) return unless fields.is_a?(Hash) return if fields.empty? fields = normalize_owned_keys(fields) if owned @source = Layer.new(@source, fields, clear_parent_deletes: true, owned: owned) invalidate_snapshot! end |
#delete(path) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/julewire/core/fields/field_stack.rb', line 204 def delete(path) return if path.empty? return unless @delete_paths_enabled @source = Layer.new(@source, {}, delete_paths: [path], clear_parent_deletes: false) invalidate_snapshot! end |
#fork ⇒ Object
174 175 176 |
# File 'lib/julewire/core/fields/field_stack.rb', line 174 def fork self.class.new(delete_paths: @delete_paths_enabled, source: @source) end |
#snapshot ⇒ Object
166 167 168 169 170 171 172 |
# File 'lib/julewire/core/fields/field_stack.rb', line 166 def snapshot return @snapshot if @snapshot_version == @version @snapshot = @source ? @source.snapshot : EMPTY_HASH @snapshot_version = @version @snapshot end |
#value_for(key, default:) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/julewire/core/fields/field_stack.rb', line 178 def value_for(key, default:) cache = @value_cache return cache[key] if cache&.key?(key) if key.is_a?(String) key = Fields::Internal.normalize_key(key) cache = @value_cache return cache[key] if cache&.key?(key) end value = source_value_for(key) return default if value.equal?(MISSING) (@value_cache ||= {})[key] = value end |
#with(fields = nil, owned: false, **keyword_fields) ⇒ Object
212 213 214 215 216 217 218 219 |
# File 'lib/julewire/core/fields/field_stack.rb', line 212 def with(fields = nil, owned: false, **keyword_fields, &) fields = field_input(fields, keyword_fields, owned: owned) return yield unless fields.is_a?(Hash) return yield if fields.empty? fields = normalize_owned_keys(fields) if owned with_layer(fields, owned: owned, &) end |
#without(path) ⇒ Object
221 222 223 224 225 226 227 |
# File 'lib/julewire/core/fields/field_stack.rb', line 221 def without(path, &) raise ArgumentError, "field path is required" if path.empty? return yield unless @delete_paths_enabled with_layer({}, delete_paths: [path], &) end |