Class: Metacosm::Simulation

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currentObject



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

def self.current
  @current ||= new
end

Instance Method Details

#apply(command) ⇒ Object



7
8
9
# File 'lib/metacosm/simulation.rb', line 7

def apply(command)
  handler_for(command).handle(command.attrs)
end

#clear!Object



30
31
32
# File 'lib/metacosm/simulation.rb', line 30

def clear!
  @events = []
end

#construct_listener_for(event) ⇒ Object (protected)



45
46
47
48
# File 'lib/metacosm/simulation.rb', line 45

def construct_listener_for(event)
  listener = Object.const_get(event.class.name.split('::').last + "Listener").new(self)
  listener
end

#eventsObject



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

def events
  @events ||= []
end

#handler_for(command) ⇒ Object (protected)



35
36
37
38
# File 'lib/metacosm/simulation.rb', line 35

def handler_for(command)
  @handlers ||= {}
  @handlers[command] ||= Object.const_get(command.class.name.split('::').last + "Handler").new
end

#listener_for(event) ⇒ Object (protected)



40
41
42
43
# File 'lib/metacosm/simulation.rb', line 40

def listener_for(event)
  @listeners ||= {}
  @listeners[event] ||= construct_listener_for(event)
end

#receive(event, record: true) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/metacosm/simulation.rb', line 11

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



3
4
5
# File 'lib/metacosm/simulation.rb', line 3

def watch(model)
  Frappuccino::Stream.new(model).on_value(&method(:receive))
end