Class: GhostWheel::Scanner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ghost_wheel/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Scanner

Returns a new instance of Scanner.



10
11
12
13
# File 'lib/ghost_wheel/scanner.rb', line 10

def initialize(text)
  @scanner   = StringScanner.new(text)
  @pos_stack = Array.new
end

Instance Method Details

#abort(return_value = nil) ⇒ Object



31
32
33
34
# File 'lib/ghost_wheel/scanner.rb', line 31

def abort(return_value = nil)
  @scanner.pos = rollback
  throw :ghost_wheel_abort_transaction, return_value
end

#transactionObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ghost_wheel/scanner.rb', line 17

def transaction
  catch(:ghost_wheel_abort_transaction) do
    @pos_stack << @scanner.pos
    begin
      result = yield self
      rollback
      result
    rescue
      @scanner.pos = rollback
      raise  # reraise exception for calling code
    end
  end
end