Class: Computable::Variable
- Inherits:
-
Object
- Object
- Computable::Variable
- Defined in:
- lib/computable.rb
Instance Attribute Summary collapse
-
#calc_method ⇒ Object
Returns the value of attribute calc_method.
-
#count ⇒ Object
Returns the value of attribute count.
-
#expired_from ⇒ Object
Returns the value of attribute expired_from.
-
#name ⇒ Object
Returns the value of attribute name.
-
#used_for ⇒ Object
Returns the value of attribute used_for.
-
#value ⇒ Object
Returns the value of attribute value.
-
#value_calced ⇒ Object
Returns the value of attribute value_calced.
Instance Method Summary collapse
- #calc! ⇒ Object
- #expire_value ⇒ Object
-
#initialize(name, calc_method) ⇒ Variable
constructor
A new instance of Variable.
- #inspect ⇒ Object
- #recalc_value ⇒ Object
- #revoke_expire ⇒ Object
Constructor Details
#initialize(name, calc_method) ⇒ Variable
Returns a new instance of Variable.
20 21 22 23 24 25 26 27 |
# File 'lib/computable.rb', line 20 def initialize(name, calc_method) @name = name @calc_method = calc_method @used_for = {} @expired_from = {} @count = 0 @value = Unknown end |
Instance Attribute Details
#calc_method ⇒ Object
Returns the value of attribute calc_method.
19 20 21 |
# File 'lib/computable.rb', line 19 def calc_method @calc_method end |
#count ⇒ Object
Returns the value of attribute count.
19 20 21 |
# File 'lib/computable.rb', line 19 def count @count end |
#expired_from ⇒ Object
Returns the value of attribute expired_from.
19 20 21 |
# File 'lib/computable.rb', line 19 def expired_from @expired_from end |
#name ⇒ Object
Returns the value of attribute name.
19 20 21 |
# File 'lib/computable.rb', line 19 def name @name end |
#used_for ⇒ Object
Returns the value of attribute used_for.
19 20 21 |
# File 'lib/computable.rb', line 19 def used_for @used_for end |
#value ⇒ Object
Returns the value of attribute value.
19 20 21 |
# File 'lib/computable.rb', line 19 def value @value end |
#value_calced ⇒ Object
Returns the value of attribute value_calced.
19 20 21 |
# File 'lib/computable.rb', line 19 def value_calced @value_calced end |
Instance Method Details
#calc! ⇒ Object
33 34 35 36 |
# File 'lib/computable.rb', line 33 def calc! self.count += 1 calc_method.call(self) end |
#expire_value ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/computable.rb', line 38 def expire_value return if used_for.empty? puts "expire #{inspect}" if Computable.computable_debug used_for.each do |name2, v2| if v2.value_calced && !v2.expired_from[name] v2.expire_value v2.expired_from[name] = self end end end |
#inspect ⇒ Object
29 30 31 |
# File 'lib/computable.rb', line 29 def inspect "<Variable #{name} used_for:#{used_for.keys} expired_from:#{expired_from.keys} has value:#{value==Unknown} value_calced:#{value_calced.inspect}>" end |
#recalc_value ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/computable.rb', line 61 def recalc_value return if !value_calced || expired_from.empty? puts "recalc #{inspect}" if Computable.computable_debug expired_from.each do |name2, v2| v2.recalc_value end unless expired_from.empty? recalced_value = self.calc! if self.value == recalced_value revoke_expire else self.value = recalced_value used_for.clear end expired_from.clear end end |
#revoke_expire ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/computable.rb', line 50 def revoke_expire return if used_for.empty? puts "revoke expire #{inspect}" if Computable.computable_debug used_for.each do |name2, v2| if v2.value_calced && v2.expired_from.delete(name) && v2.expired_from.empty? v2.revoke_expire end end end |