Class: Rubactive::ReactiveNode

Inherits:
Object
  • Object
show all
Defined in:
lib/rubactive/rubactive.rb

Overview

:nodoc:

Direct Known Subclasses

DiscreteValueStream, TimeVaryingValue

Constant Summary collapse

DEFAULT_VALUE =
:no_value_at_all

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*earlier_nodes, &recalculator) ⇒ ReactiveNode

Returns a new instance of ReactiveNode.



30
31
32
33
34
35
36
37
# File 'lib/rubactive/rubactive.rb', line 30

def initialize(*earlier_nodes, &recalculator)
  @value = DEFAULT_VALUE
  @recalculator = recalculator || ->val {val}
  @later_nodes = []
  @earlier_nodes = earlier_nodes
  @change_callback = ->ignored{}
  tell_earlier_nodes_about_me(earlier_nodes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(message, *args) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/rubactive/rubactive.rb', line 70

def method_missing(message, *args)
  recalculator = lambda do |*just_values|
    receiver = just_values.shift
    receiver.send(message, *just_values)
  end
  self.class.follows(self, *args, &recalculator)
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



26
27
28
# File 'lib/rubactive/rubactive.rb', line 26

def value
  @value
end

Class Method Details

.blankObject

test_support



90
91
92
# File 'lib/rubactive/rubactive.rb', line 90

def self.blank
  follows() {}
end

.follows(*earlier_nodes, &updater) ⇒ Object



22
23
24
# File 'lib/rubactive/rubactive.rb', line 22

def self.follows(*earlier_nodes, &updater)
  new(*earlier_nodes, &updater)
end

Instance Method Details

#just_values(args) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/rubactive/rubactive.rb', line 78

def just_values(args)
  args.collect do |arg|
    if arg.is_a?(ReactiveNode)
      arg.value
    else
      arg
    end
  end
end

#on_change(&block) ⇒ Object

When the value of this variable changes, call the block argument.



58
59
60
# File 'lib/rubactive/rubactive.rb', line 58

def on_change(&block)
  @change_callback = block
end

#propagate(value) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/rubactive/rubactive.rb', line 62

def propagate(value)
  @value = value
  @change_callback.(value)
  @later_nodes.each do |node|
    node.recalculate
  end
end

#recalculateObject



49
50
51
# File 'lib/rubactive/rubactive.rb', line 49

def recalculate
  propagate(@recalculator.call(*just_values(@earlier_nodes)))
end

#tell_earlier_nodes_about_me(earlier_nodes) ⇒ Object



39
40
41
42
43
# File 'lib/rubactive/rubactive.rb', line 39

def tell_earlier_nodes_about_me(earlier_nodes)
  earlier_nodes.each do |e|
    e.this_node_is_later_than_you(self) if e.is_a?(ReactiveNode)
  end
end

#this_node_is_later_than_you(this_node) ⇒ Object



45
46
47
# File 'lib/rubactive/rubactive.rb', line 45

def this_node_is_later_than_you(this_node)
  @later_nodes << this_node
end