Class: Stupidedi::Reader::AbstractInput

Inherits:
Object
  • Object
show all
Includes:
Inspect
Defined in:
lib/stupidedi/reader/input/abstract_input.rb

Overview

Note:

The monkey-patched classes String and Array provide compatible implementations of the abstract methods, so code written to target this interface is backward-compatable with plain unwrapped String and Array values.

Provides an abstract interface for a positioned cursor within an element-based input stream. The main operations are implemented by the #take and #drop methods.

The DelegatedInput subclass wraps values that already implement the interface, like String and Array. The FileInput subclass wraps opened IO streams like File, and possibly others.

Examples:

Reading the input

input = Input.build("abc")
input.at(0)           #=> "a"
input.take(3)         #=> "abc"

input = input.drop(1) #=> #<DelegatedInput ...>
input.at(0)           #=> "b"
input.take(3)         #=> "bc"

input = input.drop(1) #=> #<DelegatedInput ...>
input.at(0)           #=> "c"
input.take(3)         #=> "c"

Querying the position

input = Input.build("abc\ndef\nghi")
input.offset  #=> 0
input.line    #=> 1
input.column  #=> 1

input.drop(3).bind do |in|
  in.offset   #=> 3
  in.line     #=> 1
  in.column   #=> 4
end

input.drop(4).bind do |in|
  in.offset   #=> 4
  in.line     #=> 2
  in.column   #=> 1
end

Direct Known Subclasses

DelegatedInput, FileInput

Querying the Position collapse

Reading the Input collapse

Advancing the Cursor collapse

Methods included from Inspect

#inspect

Instance Method Details

#dropAbstractInput

Advance the cursor forward n elements

Parameters:

  • n (Integer)

    the number of elements to advance (n >= 0)

Returns:


118
# File 'lib/stupidedi/reader/input/abstract_input.rb', line 118

abstract :drop, :args => %w(n)

#indexInteger

Returns the smallest n, where #at(n) == element

Parameters:

  • element (Object)

    the element to find in the input

Returns:

  • (Integer)
  • nil if element is not present in the input


108
# File 'lib/stupidedi/reader/input/abstract_input.rb', line 108

abstract :index, :args => %w(element)

#positionString

The file name, URI, etc that identifies the input stream

Returns:

  • (String)

61
# File 'lib/stupidedi/reader/input/abstract_input.rb', line 61

abstract :position