Class: Roda::RodaPlugins::SinatraHelpers::DelayedBody

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/sinatra_helpers.rb

Overview

Class used when the response body is set explicitly, instead of using Roda’s default body array and response.write to write to it.

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ DelayedBody

Save the block that will return the body, it won’t be called until the body is needed.



246
247
248
# File 'lib/roda/plugins/sinatra_helpers.rb', line 246

def initialize(&block)
  @block = block
end

Instance Method Details

#eachObject

If the body is a String, yield it, otherwise yield each string returned by calling each on the body.



252
253
254
255
256
257
258
259
# File 'lib/roda/plugins/sinatra_helpers.rb', line 252

def each
  v = value
  if v.is_a?(String)
    yield v
  else
    v.each{|s| yield s}
  end
end

#empty?Boolean

Assume that if the body has been set directly that it is never empty.

Returns:

  • (Boolean)


263
264
265
# File 'lib/roda/plugins/sinatra_helpers.rb', line 263

def empty?
  false
end

#joinObject

Return the body as a single string, mostly useful during testing.



268
269
270
271
272
# File 'lib/roda/plugins/sinatra_helpers.rb', line 268

def join
  a = []
  each{|s| a << s}
  a.join
end

#lengthObject

Calculate the length for the body.



275
276
277
278
279
# File 'lib/roda/plugins/sinatra_helpers.rb', line 275

def length
  length = 0
  each{|s| length += s.bytesize}
  length
end