Class: Stupidedi::Reader::AbstractInput

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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:



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

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



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

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

#positionString

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

Returns:



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

abstract :position