Class: Relaxo::QueryServer::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/relaxo/query_server/shell.rb

Overview

A simple wrapper that reads and writes objects using JSON serialization to the given ‘input` and `output` `IO`s.

Direct Known Subclasses

MockShell

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ Shell

Returns a new instance of Shell.



28
29
30
31
# File 'lib/relaxo/query_server/shell.rb', line 28

def initialize(input, output)
	@input = input
	@output = output
end

Instance Attribute Details

#inputObject (readonly)

Returns the value of attribute input.



33
34
35
# File 'lib/relaxo/query_server/shell.rb', line 33

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



34
35
36
# File 'lib/relaxo/query_server/shell.rb', line 34

def output
  @output
end

Instance Method Details

#read_objectObject

Read a JSON serialised object from ‘input`.



37
38
39
# File 'lib/relaxo/query_server/shell.rb', line 37

def read_object
	JSON.parse @input.readline
end

#runObject

Read commands from ‘input`, execute them and then write out the results.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/relaxo/query_server/shell.rb', line 48

def run
	begin
		while true
			command = read_object

			result = yield command

			write_object(result)
		end
	rescue EOFError
		# Finish...
	end
end

#write_object(object) ⇒ Object

Write a JSON serialized object to ‘output`.



42
43
44
45
# File 'lib/relaxo/query_server/shell.rb', line 42

def write_object object
	@output.puts object.to_json
	@output.flush
end