module Is::Localized

public

Manage thread-local program state for an object.

Public Instance Methods

localize(key, value) { |value| ... } click to toggle source
public

Localize `value` for `key` in the current thread or fiber.

Be aware that you can easily introduce memory leaks into your program if you aren't careful to clean up localized state. There's two ways to approach this:

1. Pass a block to `localize`. The value will be localized and cleaned up after being yielded to the block.

2. Call `unlocalize` manually when you no longer need the localized value.
# File lib/is/localized.rb, line 22
def localize(key, value)
  key = localized_key(key)

  Core::Local::Store[key] = value

  if block_given?
    begin
      yield value
    ensure
      Core::Local::Store.delete(key)
    end
  else
    value
  end
end
localized(key, fallback = nil) click to toggle source
public

Returns the localized value for `key`, or `fallback`.

# File lib/is/localized.rb, line 40
def localized(key, fallback = nil)
  Core::Local::Store.fetch(localized_key(key), fallback)
end
unlocalize(key) click to toggle source
public

Deletes the localized value for `key`.

# File lib/is/localized.rb, line 46
def unlocalize(key)
  Core::Local::Store.delete(localized_key(key))
end

Private Instance Methods

localized_key(name) click to toggle source
# File lib/is/localized.rb, line 50
        def localized_key(name)
  "#{localized_key_prefix}_#{name}"
end
localized_key_prefix() click to toggle source
# File lib/is/localized.rb, line 54
        def localized_key_prefix
  @__localized_key_prefix ||= "__corerb_localized_#{object_id}"
end