Class List Method List File List

# Class: Concurrent::LazyRegister

Inherits:
  Synchronization::Object
  - Object
  - Synchronization::Object
  - Concurrent::LazyRegister
  show all
Defined in:
  lib/concurrent/lazy_register.rb

## Overview

Note:

Edge Feature: Edge features are under active development and may change frequently. They are expected not to keep backward compatibility (there may also lack tests and documentation). Semantic versions will be obeyed though. Features developed in `concurrent-ruby-edge` are expected to move to `concurrent-ruby` when final.

Hash-like collection that store lazys evaluated values.

Examples:

    register = Concurrent::LazyRegister.new
    #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
    register[:key]
    #=> nil
    register.add(:key) { Concurrent::Actor.spawn!(Actor::AdHoc, :ping) { -> message { message } } }
    #=> #<Concurrent::LazyRegister:0x007fd7ecd5e230 @Data=#<Concurrent::AtomicReference:0x007fd7ecd5e1e0>>
    register[:key]
    #=> #<Concurrent::Actor::Reference /ping (Concurrent::Actor::AdHoc)>

## Instance Method Summary (collapse)

- - (Object) [](key)

  Element reference.

- - (LazyRegister) initialize constructor

  A new instance of LazyRegister.

- - (LazyRegister) register(key) { ... } (also: #add, #store)

  Element assignment.

- - (true, false) registered?(key) (also: #key?, #has_key?)

  Returns true if the given key is present.

- - (LazyRegister) unregister(key) (also: #remove, #delete)

  Un-registers the object under key, realized or not.

## Constructor Details

### - (`LazyRegister`) initialize

Returns a new instance of LazyRegister

    23
    24
    25
    26

    # File 'lib/concurrent/lazy_register.rb', line 23

    def initialize
      super
      self.data = {}
    end

## Instance Method Details

### - (`Object`) [](key)

Element reference. Retrieves the value object corresponding to the key object. Returns nil if the key is not found. Raises an exception if the stored item raised an exception when the block was evaluated.

Parameters:

- key (`Object`)

Returns:

- (`Object`) —

  value stored for the key or nil if the key is not found

Raises:

- Exception when the initialization block fails

    36
    37
    38
    39

    # File 'lib/concurrent/lazy_register.rb', line 36

    def [](key)
      delay = data[key]
      delay ? delay.value! : nil
    end

### - (`LazyRegister`) register(key) { ... } Also known as: add, store

Element assignment. Associates the value given by value with the key given by key.

Parameters:

- key (`Object`)

Yields:

- the object to store under the key

Returns:

- (`LazyRegister`) —

  self

    59
    60
    61
    62
    63

    # File 'lib/concurrent/lazy_register.rb', line 59

    def register(key, &block)
      delay = Delay.new(executor: :immediate, &block)
      update_data { |h| h.merge(key => delay) }
      self
    end

### - (`true`, `false`) registered?(key) Also known as: key?, has_key?

Returns true if the given key is present.

Parameters:

- key (`Object`)

Returns:

- (`true`, `false`) —

  if the key is registered

    45
    46
    47

    # File 'lib/concurrent/lazy_register.rb', line 45

    def registered?(key)
      data.key?(key)
    end

### - (`LazyRegister`) unregister(key) Also known as: remove, delete

Un-registers the object under key, realized or not.

Parameters:

- key (`Object`)

Returns:

- (`LazyRegister`) —

  self

    73
    74
    75
    76

    # File 'lib/concurrent/lazy_register.rb', line 73

    def unregister(key)
      update_data { |h| h.dup.tap { |j| j.delete(key) } }
      self
    end