Class: Stream::ImplicitStream

Inherits:
BasicStream show all
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 { x == 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).

Constant Summary

Constants included from Stream

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#create_stream

Constructor Details

#initialize(other_stream = nil) {|self| ... } ⇒ ImplicitStream

Create a new ImplicitStream which might wrap an existing stream other_stream. If other_stream 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.

Parameters:

  • other_stream (Stream, nil) (defaults to: nil)

    optional stream to wrap

Yields:

  • (self)

    the new ImplicitStream instance for customization



632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/stream.rb', line 632

def initialize(other_stream = nil)
  # Initialize with defaults
  @at_beginning_proc = proc { true }
  @at_end_proc = proc { true }

  @set_to_begin_proc = proc {}
  @set_to_end_proc = proc {}

  if other_stream
    @wrapped_stream = other_stream
    @at_beginning_proc = proc { other_stream.at_beginning? }
    @at_end_proc = proc { other_stream.at_end? }
    @forward_proc = proc { other_stream.basic_forward }
    @backward_proc = proc { other_stream.basic_backward }
    @set_to_end_proc = proc { other_stream.set_to_end }
    @set_to_begin_proc = proc { other_stream.set_to_begin }
  end
  yield self if block_given? # let client overwrite defaults
end

Instance Attribute Details

#at_beginning_proc=(value) ⇒ Object (writeonly)

Sets the attribute at_beginning_proc

Parameters:

  • value

    the value to set the attribute at_beginning_proc to.



619
620
621
# File 'lib/stream.rb', line 619

def at_beginning_proc=(value)
  @at_beginning_proc = value
end

#at_end_proc=(value) ⇒ Object (writeonly)

Sets the attribute at_end_proc

Parameters:

  • value

    the value to set the attribute at_end_proc to.



619
620
621
# File 'lib/stream.rb', line 619

def at_end_proc=(value)
  @at_end_proc = value
end

#backward_proc=(value) ⇒ Object (writeonly)

Sets the attribute backward_proc

Parameters:

  • value

    the value to set the attribute backward_proc to.



619
620
621
# File 'lib/stream.rb', line 619

def backward_proc=(value)
  @backward_proc = value
end

#forward_proc=(value) ⇒ Object (writeonly)

Sets the attribute forward_proc

Parameters:

  • value

    the value to set the attribute forward_proc to.



619
620
621
# File 'lib/stream.rb', line 619

def forward_proc=(value)
  @forward_proc = value
end

#set_to_begin_proc=(value) ⇒ Object (writeonly)

Sets the attribute set_to_begin_proc

Parameters:

  • value

    the value to set the attribute set_to_begin_proc to.



619
620
621
# File 'lib/stream.rb', line 619

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

Parameters:

  • value

    the value to set the attribute set_to_end_proc to.



619
620
621
# File 'lib/stream.rb', line 619

def set_to_end_proc=(value)
  @set_to_end_proc = value
end

#wrapped_streamObject (readonly)

Returns the value of attribute wrapped_stream.



621
622
623
# File 'lib/stream.rb', line 621

def wrapped_stream
  @wrapped_stream
end

Instance Method Details

#at_beginning?Boolean

Returns the value of @at_beginning_proc.

Returns:

  • (Boolean)


653
654
655
# File 'lib/stream.rb', line 653

def at_beginning?
  @at_beginning_proc.call
end

#at_end?Boolean

Returns the value of @at_end_proc.

Returns:

  • (Boolean)


658
659
660
# File 'lib/stream.rb', line 658

def at_end?
  @at_end_proc.call
end

#basic_backwardObject

Returns the value of @backward_proc_proc.



668
669
670
# File 'lib/stream.rb', line 668

def basic_backward
  @backward_proc.call
end

#basic_forwardObject

Returns the value of @forward_proc.



663
664
665
# File 'lib/stream.rb', line 663

def basic_forward
  @forward_proc.call
end

#set_to_beginObject

Calls set_to_begin_proc or super if set_to_begin_proc is undefined.



678
679
680
# File 'lib/stream.rb', line 678

def set_to_begin
  @set_to_begin_proc ? @set_to_begin_proc.call : super
end

#set_to_endObject

Calls set_to_end_proc or super if set_to_end_proc is undefined.



673
674
675
# File 'lib/stream.rb', line 673

def set_to_end
  @set_to_end_proc ? @set_to_end_proc.call : super
end