Class: Metacosm::Simulation
- Inherits:
-
Object
- Object
- Metacosm::Simulation
- Defined in:
- lib/metacosm/simulation.rb
Instance Attribute Summary collapse
-
#running ⇒ Object
Returns the value of attribute running.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(command) ⇒ Object
- #clear! ⇒ Object
- #command_queue ⇒ Object
- #conduct! ⇒ Object
- #construct_listener_for(event) ⇒ Object protected
- #events ⇒ Object
- #execute ⇒ Object
- #fire(command) ⇒ Object
- #halt! ⇒ Object
- #handler_for(command) ⇒ Object protected
- #listener_for(event) ⇒ Object protected
- #mutex ⇒ Object
- #receive(event, record: true) ⇒ Object
- #watch(model) ⇒ Object
Instance Attribute Details
#running ⇒ Object
Returns the value of attribute running.
3 4 5 |
# File 'lib/metacosm/simulation.rb', line 3 def running @running end |
Class Method Details
.current ⇒ Object
60 61 62 |
# File 'lib/metacosm/simulation.rb', line 60 def self.current @current ||= new end |
Instance Method Details
#apply(command) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/metacosm/simulation.rb', line 38 def apply(command) #mutex.synchronize do # p [ :applying, command: command ] handler_for(command).handle(command.attrs) #end end |
#clear! ⇒ Object
64 65 66 |
# File 'lib/metacosm/simulation.rb', line 64 def clear! @events = [] end |
#command_queue ⇒ Object
13 14 15 |
# File 'lib/metacosm/simulation.rb', line 13 def command_queue @command_queue ||= Queue.new end |
#conduct! ⇒ Object
17 18 19 |
# File 'lib/metacosm/simulation.rb', line 17 def conduct! @conductor_thread = Thread.new { execute } end |
#construct_listener_for(event) ⇒ Object (protected)
79 80 81 82 |
# File 'lib/metacosm/simulation.rb', line 79 def construct_listener_for(event) listener = Object.const_get(event.class.name.split('::').last + "Listener").new(self) listener end |
#events ⇒ Object
56 57 58 |
# File 'lib/metacosm/simulation.rb', line 56 def events @events ||= [] end |
#execute ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/metacosm/simulation.rb', line 21 def execute while true if (command=command_queue.pop) apply(command) end sleep 0.01 end end |
#fire(command) ⇒ Object
8 9 10 11 |
# File 'lib/metacosm/simulation.rb', line 8 def fire(command) # p [ :firing, command: command ] command_queue.push(command) end |
#halt! ⇒ Object
30 31 32 |
# File 'lib/metacosm/simulation.rb', line 30 def halt! @conductor_thread.terminate end |
#handler_for(command) ⇒ Object (protected)
69 70 71 72 |
# File 'lib/metacosm/simulation.rb', line 69 def handler_for(command) @handlers ||= {} @handlers[command] ||= Object.const_get(command.class.name.split('::').last + "Handler").new end |
#listener_for(event) ⇒ Object (protected)
74 75 76 77 |
# File 'lib/metacosm/simulation.rb', line 74 def listener_for(event) @listeners ||= {} @listeners[event] ||= construct_listener_for(event) end |
#mutex ⇒ Object
34 35 36 |
# File 'lib/metacosm/simulation.rb', line 34 def mutex @mutex = Mutex.new end |
#receive(event, record: true) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/metacosm/simulation.rb', line 45 def receive(event, record: true) events.push(event) if record listener = listener_for(event) if event.attrs.any? listener.receive(event.attrs) else listener.receive end end |
#watch(model) ⇒ Object
4 5 6 |
# File 'lib/metacosm/simulation.rb', line 4 def watch(model) Frappuccino::Stream.new(model).on_value(&method(:receive)) end |