Class: Roda::RodaPlugins::SinatraHelpers::DelayedBody
- Inherits:
-
Object
- Object
- Roda::RodaPlugins::SinatraHelpers::DelayedBody
- 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
-
#each ⇒ Object
If the body is a String, yield it, otherwise yield each string returned by calling each on the body.
-
#empty? ⇒ Boolean
Assume that if the body has been set directly that it is never empty.
-
#initialize(&block) ⇒ DelayedBody
constructor
Save the block that will return the body, it won’t be called until the body is needed.
-
#join ⇒ Object
Return the body as a single string, mostly useful during testing.
-
#length ⇒ Object
Calculate the length for the body.
Constructor Details
#initialize(&block) ⇒ DelayedBody
Save the block that will return the body, it won’t be called until the body is needed.
232 233 234 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 232 def initialize(&block) @block = block end |
Instance Method Details
#each ⇒ Object
If the body is a String, yield it, otherwise yield each string returned by calling each on the body.
238 239 240 241 242 243 244 245 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 238 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.
249 250 251 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 249 def empty? false end |
#join ⇒ Object
Return the body as a single string, mostly useful during testing.
254 255 256 257 258 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 254 def join a = [] each{|s| a << s} a.join end |
#length ⇒ Object
Calculate the length for the body.
261 262 263 264 265 |
# File 'lib/roda/plugins/sinatra_helpers.rb', line 261 def length length = 0 each{|s| length += s.bytesize} length end |