class Rubactive::DiscreteValueStream

A DiscreteValueStream represents a stream of values. When a value is added to the stream, other DiscreteValueStreams may react if they follow this stream.

Streams are explicitly created with DiscreteValueStream.manual or DiscreteValueStream.follows. In the first case, values are added only with add_value. In the second, values can also be added in reaction to the streams being followed.

Streams can also be implicity created by sending other streams unrecognized messages.

origin = DiscreteValueStream.manual
follower = origin + 1

The previous definition of the follower stream is equivalent to this:

follower = DiscreteValueStream.follows(origin) { | o | o + 1 }

Public Class Methods

follows(*streams, &block) click to toggle source

Create a stream that reacts to one or more other streams.

The addition of values to any of the streams will cause the block to be called with their most recent values. The result is added to this stream (as with add_value).

Example:

origin = DiscreteValueStream.manual
follower = DiscreteValueStream.follows(origin) { | o | o+1 }
origin.add_value(5)
follower.most_recent_value #=> 6

If no block is given, this stream should be following only one other stream. The value just added to that stream is also added to this one.

Calls superclass method
# File lib/rubactive/rubactive.rb, line 214
def self.follows(*streams, &block)
  super
end
manual() click to toggle source

Create an empty value stream

Use add_value to insert values into the stream.

# File lib/rubactive/rubactive.rb, line 221
def self.manual
  follows {
    raise "Incorrect use of recalculation in a manual event stream"
  }
end

Public Instance Methods

add_value(new_value) click to toggle source

Place a new value on the stream

# File lib/rubactive/rubactive.rb, line 244
def add_value(new_value)    # this?
  self.value = new_value
end
empty?() click to toggle source

True iff no value has ever been added to the stream.

# File lib/rubactive/rubactive.rb, line 249
def empty?
  most_recent_value == DEFAULT_VALUE
end
most_recent_value() click to toggle source

Retrieve last value added to the stream

Earlier values are inaccessible.

It is an error to ask for the value of an empty? stream.

# File lib/rubactive/rubactive.rb, line 241
def most_recent_value; @value; end
on_addition(&callback) click to toggle source

Run a callback when a new value is added.

This is an interface to the non-reactive world. When a new value is added (whether with add_value or in reaction to a followed stream), the callback is called and given that value. The callback will typically do something with the value, like add it to a GUI.

# File lib/rubactive/rubactive.rb, line 233
def on_addition(&callback); on_change(&callback); end