Class: Reac

Inherits:
Object
  • Object
show all
Defined in:
lib/reac/reactivize.rb,
lib/reac.rb,
lib/reac/view.rb,
lib/reac/view/sdl.rb,
lib/reac/view/curses.rb

Overview

Reac::Reactivize

Direct Known Subclasses

Array, Call, Proc, Value

Defined Under Namespace

Modules: Reactivize Classes: Array, Call, CursesView, Proc, SDLView, Value, View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

construct tree of Reac::*

(instead of call the method)

to call it later



14
15
16
# File 'lib/reac.rb', line 14

def method_missing(method, *args)
  Call.new(self, method, args)
end

Instance Attribute Details

#last_updateObject

Returns the value of attribute last_update.



9
10
11
# File 'lib/reac.rb', line 9

def last_update
  @last_update
end

Class Method Details

.value(reac, tick = nil) ⇒ Object

caluculate the value by traversing the tree



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/reac.rb', line 25

def self.value(reac, tick=nil)
  return reac if not reac.is_a?(Reac) 

  # return cached data if already calculated
  return reac.data if tick && (reac.last_update == tick)

  reac.last_update = tick 
  case reac
  when Value
    reac.data
  when Call
    receiver = Reac.value(reac.receiver, tick)
    args = reac.args.map{|item|
      Reac.value(item, tick)
    }
    reac.data = receiver.__send__(reac.method, *args)
  when Proc
    reac.data = reac.proc.call
  when Array
    reac.data = reac.ary.map{|item| Reac.value(item, tick)}
  else
    raise "must not happen"
  end
end

Instance Method Details

#valueObject

caluculate the value (short-hand for Reac.value(x))



20
21
22
# File 'lib/reac.rb', line 20

def value
  Reac.value(self)
end