Class: SuperSettings::Context::Current

Inherits:
Object
  • Object
show all
Defined in:
lib/super_settings/context/current.rb

Overview

Current context for maintaining consistent setting values and random numbers within a specific execution context.

Instance Method Summary collapse

Constructor Details

#initializeCurrent

Returns a new instance of Current.



8
9
10
11
# File 'lib/super_settings/context/current.rb', line 8

def initialize
  @context = {}
  @seed = nil
end

Instance Method Details

#[](key) ⇒ Object

Get a value from the context.

Parameters:

  • key (String)

    the key to retrieve

Returns:

  • (Object)

    the value associated with the key



25
26
27
# File 'lib/super_settings/context/current.rb', line 25

def [](key)
  @context[key]
end

#[]=(key, value) ⇒ Object

Set a value in the context.

Parameters:

  • key (String)

    the key to set

  • value (Object)

    the value to store

Returns:

  • (Object)

    the stored value



34
35
36
# File 'lib/super_settings/context/current.rb', line 34

def []=(key, value)
  @context[key] = value
end

#delete(key) ⇒ Object

Delete a value from the context.

Parameters:

  • key (String)

    the key to delete

Returns:

  • (Object)

    the deleted value



42
43
44
# File 'lib/super_settings/context/current.rb', line 42

def delete(key)
  @context.delete(key)
end

#include?(key) ⇒ Boolean

Check if the context includes a specific key.

Parameters:

  • key (String)

    the key to check

Returns:

  • (Boolean)

    true if the key exists in the context



17
18
19
# File 'lib/super_settings/context/current.rb', line 17

def include?(key)
  @context.include?(key)
end

#rand(max = nil) ⇒ Integer, Float

Generate a consistent random number for this context.

Parameters:

  • max (Integer, Float, Range) (defaults to: nil)

    the maximum value or range

Returns:

  • (Integer, Float)

    the random number



50
51
52
53
# File 'lib/super_settings/context/current.rb', line 50

def rand(max = nil)
  @seed ||= Random.new_seed
  Random.new(@seed).rand(max || 1.0)
end