Class: Webgen::Path::SourceIO
- Inherits:
-
Object
- Object
- Webgen::Path::SourceIO
- Defined in:
- lib/webgen/path.rb
Overview
Helper class for easy access to the content of a path.
This class is used sothat the creation of the real IO object for #stream can be delayed till it is actually needed. This is done by not directly requiring the user of this class to supply the IO object, but by requiring a block that creates the real IO object.
Instance Method Summary collapse
-
#data ⇒ Object
Return the whole content of the wrapped IO object as string.
-
#initialize(&block) ⇒ SourceIO
constructor
Create a new SourceIO object.
-
#stream ⇒ Object
Provide direct access to the wrapped IO object by yielding it.
Constructor Details
#initialize(&block) ⇒ SourceIO
Create a new SourceIO object. A block has to be specified that returns the to-be-wrapped IO object.
45 46 47 48 |
# File 'lib/webgen/path.rb', line 45 def initialize(&block) @block = block raise ArgumentError, 'You need to provide a block which returns an IO object' if @block.nil? end |
Instance Method Details
#data ⇒ Object
Return the whole content of the wrapped IO object as string.
60 61 62 |
# File 'lib/webgen/path.rb', line 60 def data stream {|io| io.read} end |
#stream ⇒ Object
Provide direct access to the wrapped IO object by yielding it. After the method block returns the IO object is automatically closed.
52 53 54 55 56 57 |
# File 'lib/webgen/path.rb', line 52 def stream io = @block.call yield(io) ensure io.close end |