Class: Stupidedi::Reader::AbstractInput

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

Overview

Note:

The String and Array refinements in lib/ruby 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

Testing the Input collapse

Methods included from Inspect

#inspect

Instance Method Details

#==(other) ⇒ Boolean

This method is abstract.

True if other equals the remaining input

Returns:

  • (Boolean)


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

abstract :==, :args => %w(other)

#at(n)

This method is abstract.

Read a single element at the given index. Result is undefined unless the input contains enough elements, which can be tested with #defined_at?

Parameters:

  • n (Integer)

    the index of the element to read (n >= 0)



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

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

#defined_at?(n) ⇒ Boolean

This method is abstract.

True if the input contains enough elements such that #at(n) is defined

Parameters:

  • n (Integer)

    the index to test (n >= 0)

Returns:

  • (Boolean)


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

abstract :defined_at?, :args => %w(n)

#drop(n) ⇒ AbstractInput

Advance the cursor forward n elements

Parameters:

  • n (Integer)

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

Returns:



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

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

#empty?Boolean

This method is abstract.

True if no elements remain in the input

Returns:

  • (Boolean)


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

abstract :empty?

#index(element) ⇒ Integer

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



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

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

#positionString

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

Returns:

  • (String)


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

abstract :position

#take(n)

This method is abstract.

Read the first n elements

Parameters:

  • n (Integer)

    number of elements to read (n >= 0)



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

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