Class: Relaxo::QueryServer::Shell
- Inherits:
-
Object
- Object
- Relaxo::QueryServer::Shell
- 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
Instance Attribute Summary collapse
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
-
#initialize(input, output) ⇒ Shell
constructor
A new instance of Shell.
-
#read_object ⇒ Object
Read a JSON serialised object from ‘input`.
-
#run ⇒ Object
Read commands from ‘input`, execute them and then write out the results.
-
#write_object(object) ⇒ Object
Write a JSON serialized object to ‘output`.
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
#input ⇒ Object (readonly)
Returns the value of attribute input.
33 34 35 |
# File 'lib/relaxo/query_server/shell.rb', line 33 def input @input end |
#output ⇒ Object (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_object ⇒ Object
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 |
#run ⇒ Object
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 |