Class: Qwe::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/qwe/console.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = 0, port = nil) ⇒ Console

Returns a new instance of Console.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/qwe/console.rb', line 11

def initialize(id = 0, port = nil)
  @id = id.to_i
  @port = if port
    port.to_i
  else
    Qwe::DB::Server::DEFAULT_PORT
  end

  Qwe::DB.connect("druby://localhost:#{port}") if port
  setup
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#portObject (readonly)

Returns the value of attribute port.



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

def port
  @port
end

Class Method Details

.startObject



5
6
7
# File 'lib/qwe/console.rb', line 5

def self.start(...)
  new(...).start
end

Instance Method Details

#record_procObject



53
54
55
56
57
58
# File 'lib/qwe/console.rb', line 53

def record_proc
  i = id
  proc do |statements, *args|
    Qwe::DB[i].record.evaluate(statements)
  end
end

#setupObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qwe/console.rb', line 23

def setup
  if id > 0
    IRB::WorkSpace.define_method(:evaluate, &record_proc)
    req = Qwe::DB[id].record.evaluate("Worker.instance.requirements")
    msg = "\e[1;35mQwe interactive prompt\e[0m, record \e[1;32m##{id}\e[0m"
  else
    IRB::WorkSpace.define_method(:evaluate, &worker_proc)
    req = Qwe::DB.server.pick_worker.requirements
    msg = "\e[1;35mQwe interactive prompt\e[0m, random worker"
  end

  puts msg
  require req if req
end

#startObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/qwe/console.rb', line 42

def start
  binding.irb(show_code: false)
rescue
  # Older irb versions always print source code around call point
  IRB.setup(source_location[0], argv: [])
  workspace = IRB::WorkSpace.new(self)
  binding_irb = IRB::Irb.new(workspace)
  binding_irb.context.irb_path = File.expand_path(source_location[0])
  binding_irb.run(IRB.conf)
end

#to_sObject



38
39
40
# File 'lib/qwe/console.rb', line 38

def to_s
  "Qwe id=#{id}"
end

#worker_procObject



60
61
62
63
64
# File 'lib/qwe/console.rb', line 60

def worker_proc
  proc do |statements, *args|
    Qwe::DB.server.pick_worker.evaluate(statements)
  end
end