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

#columnInteger

The column of the current position. The column resets to ‘1` each time a newline is read

Returns:



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

delegate :column, :to => :position

#dropAbstractInput

Advance the cursor forward ‘n` elements

Parameters:

  • n (Integer)

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

Returns:



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

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



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

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

#lineInteger

The line of the current position

Returns:



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

delegate :line, :to => :position

#offsetInteger

The current position as the number of elements previously read

Returns:



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

delegate :offset, :to => :position

#pathString

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

Returns:



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

delegate :path, :to => :position

#positionPosition

The Position value that describes the position of the input stream

Returns:



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

abstract :position