Class: Stream::ImplicitStream
- Inherits:
-
BasicStream
- Object
- BasicStream
- Stream::ImplicitStream
- Defined in:
- lib/stream.rb
Overview
An ImplicitStream is an easy way to create a stream on the fly without defining a subclass of BasicStream. The basic methods required for a stream are defined with blocks:
s = Stream::ImplicitStream.new { |s|
x = 0 s.at_end_proc = proc == 5 s.forward_proc = proc {x += 1 }
}
s.to_a ==> [1, 2, 3, 4, 5]
Note that this stream is only partially defined since backward_proc and at_beginning_proc are not defined. It may as well be useful if only moving forward is required by the code fragment.
ImplicitStreams can be based on other streams using the method modify which is for example used in the methods for creating stream wrappers which remove the first or last element of an existing stream (see remove_first and remove_last).
Instance Attribute Summary collapse
-
#at_beginning_proc ⇒ Object
writeonly
Sets the attribute at_beginning_proc.
-
#at_end_proc ⇒ Object
writeonly
Sets the attribute at_end_proc.
-
#backward_proc ⇒ Object
writeonly
Sets the attribute backward_proc.
-
#forward_proc ⇒ Object
writeonly
Sets the attribute forward_proc.
-
#set_to_begin_proc ⇒ Object
writeonly
Sets the attribute set_to_begin_proc.
-
#set_to_end_proc ⇒ Object
writeonly
Sets the attribute set_to_end_proc.
-
#wrapped_stream ⇒ Object
readonly
Returns the value of attribute wrapped_stream.
Instance Method Summary collapse
-
#at_beginning? ⇒ Boolean
Returns the value of @at_beginning_proc.
-
#at_end? ⇒ Boolean
Returns the value of @at_end_proc.
-
#basic_backward ⇒ Object
Returns the value of @backward_proc_proc.
-
#basic_forward ⇒ Object
Returns the value of @forward_proc.
-
#initialize(otherStream = nil) {|_self| ... } ⇒ ImplicitStream
constructor
Create a new ImplicitStream which might wrap an existing stream otherStream.
-
#set_to_begin ⇒ Object
Calls set_to_begin_proc or super if set_to_begin_proc is undefined.
-
#set_to_end ⇒ Object
Calls set_to_end_proc or super if set_to_end_proc is undefined.
Methods included from Stream
#+, #backward, #collect, #concatenate, #concatenate_collected, #create_stream, #current, #current_edge, #each, #empty?, #filtered, #first, #forward, #last, #modify, #move_backward_until, #move_forward_until, #peek, #remove_first, #remove_last, #reverse, #unwrapped
Methods included from Enumerable
Constructor Details
#initialize(otherStream = nil) {|_self| ... } ⇒ ImplicitStream
Create a new ImplicitStream which might wrap an existing stream otherStream. If otherStream is supplied the blocks for the basic stream methods are initialized with closures that delegate all operations to the wrapped stream.
If a block is given to new, than it is called with the new ImplicitStream stream as parameter letting the client overwriting the default blocks.
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/stream.rb', line 470 def initialize (otherStream=nil) if otherStream @wrapped_stream = otherStream @at_beginning_proc = proc {otherStream.at_beginning?} @at_end_proc = proc {otherStream.at_end?} @forward_proc = proc {otherStream.basic_forward} @backward_proc = proc {otherStream.basic_backward} @set_to_end_proc = proc {otherStream.set_to_end} @set_to_begin_proc = proc {otherStream.set_to_begin} end yield self if block_given? # let client overwrite defaults @at_beginning_proc = proc {true} unless @at_beginning_proc @at_end_proc = proc {true} unless @at_end_proc end |
Instance Attribute Details
#at_beginning_proc=(value) ⇒ Object (writeonly)
Sets the attribute at_beginning_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def at_beginning_proc=(value) @at_beginning_proc = value end |
#at_end_proc=(value) ⇒ Object (writeonly)
Sets the attribute at_end_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def at_end_proc=(value) @at_end_proc = value end |
#backward_proc=(value) ⇒ Object (writeonly)
Sets the attribute backward_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def backward_proc=(value) @backward_proc = value end |
#forward_proc=(value) ⇒ Object (writeonly)
Sets the attribute forward_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def forward_proc=(value) @forward_proc = value end |
#set_to_begin_proc=(value) ⇒ Object (writeonly)
Sets the attribute set_to_begin_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def set_to_begin_proc=(value) @set_to_begin_proc = value end |
#set_to_end_proc=(value) ⇒ Object (writeonly)
Sets the attribute set_to_end_proc
460 461 462 |
# File 'lib/stream.rb', line 460 def set_to_end_proc=(value) @set_to_end_proc = value end |
#wrapped_stream ⇒ Object (readonly)
Returns the value of attribute wrapped_stream.
461 462 463 |
# File 'lib/stream.rb', line 461 def wrapped_stream @wrapped_stream end |
Instance Method Details
#at_beginning? ⇒ Boolean
Returns the value of @at_beginning_proc.
487 |
# File 'lib/stream.rb', line 487 def at_beginning?; @at_beginning_proc.call; end |
#at_end? ⇒ Boolean
Returns the value of @at_end_proc.
489 |
# File 'lib/stream.rb', line 489 def at_end?; @at_end_proc.call; end |
#basic_backward ⇒ Object
Returns the value of @backward_proc_proc.
494 |
# File 'lib/stream.rb', line 494 def basic_backward; @backward_proc.call; end |
#basic_forward ⇒ Object
Returns the value of @forward_proc.
492 |
# File 'lib/stream.rb', line 492 def basic_forward; @forward_proc.call; end |
#set_to_begin ⇒ Object
Calls set_to_begin_proc or super if set_to_begin_proc is undefined.
502 503 504 |
# File 'lib/stream.rb', line 502 def set_to_begin @set_to_begin_proc ? @set_to_begin_proc.call : super end |
#set_to_end ⇒ Object
Calls set_to_end_proc or super if set_to_end_proc is undefined.
497 498 499 |
# File 'lib/stream.rb', line 497 def set_to_end @set_to_end_proc ? @set_to_end_proc.call : super end |