class Roda::RodaPlugins::SinatraHelpers::DelayedBody
Class used when the response body is set explicitly, instead of using Roda’s default body array and response.write to write to it.
Public Class Methods
Source
# File lib/roda/plugins/sinatra_helpers.rb, line 246 def initialize(&block) @block = block end
Save the block that will return the body, it won’t be called until the body is needed.
Public Instance Methods
Source
# 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
If the body is a String, yield it, otherwise yield each string returned by calling each on the body.
Source
# File lib/roda/plugins/sinatra_helpers.rb, line 263 def empty? false end
Assume that if the body has been set directly that it is never empty.
Source
# File lib/roda/plugins/sinatra_helpers.rb, line 268 def join a = [] each{|s| a << s} a.join end
Return the body as a single string, mostly useful during testing.
Source
# File lib/roda/plugins/sinatra_helpers.rb, line 275 def length length = 0 each{|s| length += s.bytesize} length end
Calculate the length for the body.
Private Instance Methods
Source
# File lib/roda/plugins/sinatra_helpers.rb, line 285 def value @value ||= @block.call end
Cache the body returned by the block. This way the block won’t be called multiple times.