Class: Computron::Simulation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Simulation

Returns a new instance of Simulation.



5
6
7
8
# File 'lib/computron/simulation.rb', line 5

def initialize(&block)
  @client     = Client.new
  @simulation = Proc.new { instance_eval(&block) }
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#default_host(host = nil) ⇒ Object



18
19
20
# File 'lib/computron/simulation.rb', line 18

def default_host(host=nil)
  host ? @default_host = host : @default_host
end

#every(interval, opts = {}, &block) ⇒ Object



22
23
24
25
26
27
# File 'lib/computron/simulation.rb', line 22

def every(interval, opts={}, &block)
  timer = EventMachine::PeriodicTimer.new(0) do
    timer.interval = interval.respond_to?(:call) ? interval.call : interval
    block.call(timer)
  end
end

#halt!Object



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

def halt!
  EM::stop
end

#run!Object



10
11
12
# File 'lib/computron/simulation.rb', line 10

def run!
  EM::run { @simulation.call }
end

#url(path) ⇒ Object

Deal with paths and URLs



38
39
40
41
42
43
# File 'lib/computron/simulation.rb', line 38

def url(path)
  url = String.new
  url.concat "http://#{default_host}" unless url =~ %r{^(\w+)://} if default_host
  url.concat path =~ /^\// ? path : "/#{path}" # Deal with leading /\
  url
end