Class: Rubactive::TimeVaryingValue
- Inherits:
-
ReactiveNode
- Object
- ReactiveNode
- Rubactive::TimeVaryingValue
- Defined in:
- lib/rubactive/rubactive.rb
Overview
A TimeVaryingValue represents a value that might “mysteriously” change each time you look at it.
The current value can change in three ways:
-
Explicitly, via change_to. That’s no different than setting an attribute of any sort of object.
-
It might have been created to “follow” other time-varying values. In that case, it will react to any of their changes by recalculating itself (in a way defined when it was created.)
-
It might have been created to follow a DiscreteValueStream. In that case, any value added to the stream becomes the time-varying value’s current value.
There is a different constructor for each of the above cases. In addition, variables can be implicity created by sending unrecognized messages to other time-varying values.
origin = TimeVaryingValue.starting_with(5)
follower = origin + 1
follower.current #=> 6
origin.change_to(700)
follower.current #=> 701
Constant Summary
Constants inherited from ReactiveNode
Instance Attribute Summary
Attributes inherited from ReactiveNode
Class Method Summary collapse
-
.follows(*values, &block) ⇒ Object
Create a value that changes as the values it depends upon change.
-
.starting_with(initial_value) ⇒ Object
Create a new TimeVaryingValue.
-
.tracks_stream(value_stream, initial_value) ⇒ Object
Create a time-varying value that follows the latest value in a stream.
Instance Method Summary collapse
-
#change_to(new_value) ⇒ Object
Change the current value.
-
#current ⇒ Object
Retrieve the current value.
-
#initialize(*earlier_nodes, &recalculator) ⇒ TimeVaryingValue
constructor
:nodoc:.
Methods inherited from ReactiveNode
blank, #just_values, #method_missing, #on_change, #propagate, #recalculate, #tell_earlier_nodes_about_me, #this_node_is_later_than_you
Constructor Details
#initialize(*earlier_nodes, &recalculator) ⇒ TimeVaryingValue
:nodoc:
174 175 176 177 |
# File 'lib/rubactive/rubactive.rb', line 174 def initialize(*earlier_nodes, &recalculator) # :nodoc: super recalculate end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rubactive::ReactiveNode
Class Method Details
.follows(*values, &block) ⇒ Object
Create a value that changes as the values it depends upon change.
When any followed variables change, the block is called with their current values. The result becomes the current value for this TimeVaryingValue.
Example:
verb = TimeVaryingValue.starting_with("vote")
adverb = TimeVaryingValue.starting_with("early")
tracker = TimeVaryingValue.follows(verb, adverb) { | v, a | "#{v} #{a}!" }
tracker.current #=> "vote early!"
adverb.change_to("often")
tracker.current #=> "vote often!"
If no block is given, this value should be following only one other. It adopts that other value whenever it changes. – Defined here so that I can write special-purpose documentation.
139 140 141 |
# File 'lib/rubactive/rubactive.rb', line 139 def self.follows(*values, &block) super end |
.starting_with(initial_value) ⇒ Object
Create a new TimeVaryingValue.
Since the returned instance follows nothing, only change_to can be used to change its value.
147 148 149 |
# File 'lib/rubactive/rubactive.rb', line 147 def self.starting_with(initial_value) follows { initial_value } end |
.tracks_stream(value_stream, initial_value) ⇒ Object
Create a time-varying value that follows the latest value in a stream.
The first argument must be a DiscreteValueStream. Whenever that stream has a new value added, the time-varying value changes to match.
The time-varying value always starts with the given initial_value, even if stream has previously had some values added to it. – Flapjax startsWith
160 161 162 163 164 |
# File 'lib/rubactive/rubactive.rb', line 160 def self.tracks_stream(value_stream, initial_value) retval = follows(value_stream) retval.change_to(initial_value) retval end |
Instance Method Details
#change_to(new_value) ⇒ Object
Change the current value.
172 |
# File 'lib/rubactive/rubactive.rb', line 172 def change_to(new_value); self.value=new_value; end |
#current ⇒ Object
Retrieve the current value.
Earlier values are inaccessible.
169 |
# File 'lib/rubactive/rubactive.rb', line 169 def current; value; end |