Class: Bogo::Stack::Context

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/bogo/stack.rb

Overview

Context for the stack execution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, stack:) ⇒ Stack

Create a new context

Parameters:

  • stack (Stack)

    initial stack associated to this 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

#stacksArray<Stack> (readonly)

Returns list of stacks associated to context.

Returns:

  • (Array<Stack>)

    list of stacks associated to context



201
202
203
# File 'lib/bogo/stack.rb', line 201

def stacks
  @stacks
end

Instance Method Details

#delete(*path, key) ⇒ Object?

Delete the key from the path

Parameters:

  • path (String, Symbol)

    path to Hash

  • key (String, Symbol)

    key to delete

Returns:

  • (Object, nil)

    removed value



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

Parameters:

Returns:

  • (self)


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.

Parameters:

  • key (String, Symbol)

    path to value location

Returns:

  • (Object)


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

Parameters:

  • key (String, Symbol)

    path to value location

Returns:

  • (Object, nil)


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.

Returns:

  • (Boolean)


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

Parameters:

  • key (String, Symbol)

    path to value location

  • value (Object)

    value to store

Returns:

  • (Object)

    value



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