Class: Relaxo::QueryServer::ListRenderer

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

Overview

Supports the ‘list` action which consumes rows and outputs encoded text in chunks.

Instance Method Summary collapse

Methods inherited from Process

#call, #log

Constructor Details

#initialize(context, function) ⇒ ListRenderer

Returns a new instance of ListRenderer.



59
60
61
62
63
64
65
66
# File 'lib/relaxo/query_server/designer.rb', line 59

def initialize(context, function)
  super(context, function)
  
  @started = false
  @fetched_row = false
  @start_response = {:headers => {}}
  @chunks = []
end

Instance Method Details

#eachObject



82
83
84
85
86
# File 'lib/relaxo/query_server/designer.rb', line 82

def each
  while row = get_row
    yield row
  end
end

#flushObject



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/relaxo/query_server/designer.rb', line 110

def flush
  if @started
    @context.shell.write_object ["chunks", @chunks]
  else
    @context.shell.write_object ["start", @chunks, @start_response]
    
    @started = true
  end
  
  @chunks = []
end

#get_rowObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/relaxo/query_server/designer.rb', line 88

def get_row
  flush
  
  row = @context.shell.read_object
  @fetched_row = true
  
  case command = row[0]
  when "list_row"
    row[1]
  when "list_end"
    false
  else
    raise RuntimeError.new("Input is not a row!")
  end
end

#run(head, request) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/relaxo/query_server/designer.rb', line 68

def run(head, request)
  super(head, request)
  
  # Ensure that at least one row is read from input:
  get_row unless @fetched_row
  
  return ["end", @chunks]
end

#send(chunk) ⇒ Object



77
78
79
80
# File 'lib/relaxo/query_server/designer.rb', line 77

def send(chunk)
  @chunks << chunk
  false
end

#start(response) ⇒ Object

Raises:

  • (RuntimeError)


104
105
106
107
108
# File 'lib/relaxo/query_server/designer.rb', line 104

def start(response)
  raise RuntimeError.new("List already started!") if @started
  
  @start_response = response
end