Class: Bogo::Stack::Context
- Inherits:
-
Object
- Object
- Bogo::Stack::Context
- Includes:
- MonitorMixin
- Defined in:
- lib/bogo/stack.rb
Overview
Context for the stack execution
Instance Attribute Summary collapse
-
#stacks ⇒ Array<Stack>
readonly
List of stacks associated to context.
Instance Method Summary collapse
-
#delete(*path, key) ⇒ Object?
Delete the key from the path.
-
#for(stack) ⇒ self
Associate stack with this context.
-
#get(*key) ⇒ Object
Fetch stored value from key location.
-
#grab(*key) ⇒ Object?
Fetch stored value from key location.
-
#initialize(*args, stack:) ⇒ Stack
constructor
Create a new context.
-
#is_set?(*key) ⇒ Boolean
Check if value is set.
-
#set(*key, value) ⇒ Object
Store value at key location.
Constructor Details
#initialize(*args, stack:) ⇒ Stack
Create a new context
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/bogo/stack.rb', line 207 def initialize(*args, stack:) super() if !stack.is_a?(Stack) raise TypeError, "Expecting `#{Stack.name}` but received `#{stack.class.name}`" end @stacks = [stack].freeze @data = Smash.new freeze_data! end |
Instance Attribute Details
Instance Method Details
#delete(*path, key) ⇒ Object?
Delete the key from the path
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/bogo/stack.rb', line 292 def delete(*path, key) synchronize do e_val = @data.get(*path, key) return if e_val.nil? || e_val.is_a?(MonitorMixin::ConditionVariable) new_data = @data.to_smash base = new_data.get(*path) base.delete(key) @data = new_data.to_smash(:freeze).freeze e_val end end |
#for(stack) ⇒ self
Associate stack with this context
222 223 224 225 |
# File 'lib/bogo/stack.rb', line 222 def for(stack) @stacks = @stacks.dup.push(stack).freeze self end |
#get(*key) ⇒ Object
Fetch stored value from key location. If value is not set, will wait until value is available.
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/bogo/stack.rb', line 244 def get(*key) synchronize do val = @data.get(*key) return val if !val.nil? val = new_cond set(*key, val) val.wait @data.get(*key) end end |
#grab(*key) ⇒ Object?
Fetch stored value from key location. if value is not set, will return nil immediately
260 261 262 263 264 |
# File 'lib/bogo/stack.rb', line 260 def grab(*key) synchronize do @data.get(*key) end end |
#is_set?(*key) ⇒ Boolean
Check if value is set.
230 231 232 233 234 235 236 237 |
# File 'lib/bogo/stack.rb', line 230 def is_set?(*key) synchronize do val = @data.get(*key) return false if val.nil? return false if val.is_a?(MonitorMixin::ConditionVariable) true end end |
#set(*key, value) ⇒ Object
Store value at key location
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/bogo/stack.rb', line 271 def set(*key, value) synchronize do return delete(*key) if value.nil? && !@data.get(*key).is_a?(MonitorMixin::ConditionVariable) e_val = @data.get(*key) new_data = @data.to_smash new_data.set(*key, value) @data = new_data.to_smash(:freeze).freeze if e_val.is_a?(MonitorMixin::ConditionVariable) e_val.broadcast end value end end |