Included Modules

Files

Interactive::Rewindable

Copyright © 2012 Alex Suraci

Public Instance Methods

ask(question, options = {}) click to toggle source

Ask a question and get an answer. Rewind-aware; call disable_rewind on your class to disable.

See Interact#ask for the other possible values in options.

question

The prompt, without “: ” at the end.

options

An optional hash containing the following options.

forget

Set to true to prevent rewinding from remembering the user’s answer.

# File lib/interact/rewindable.rb, line 39
def ask(question, options = {})
  rewind = HAS_CALLCC

  if rewind
    prompt, answer = callcc { |cc| [cc, nil] }
  else
    prompt, answer = nil, nil
  end

  if answer
    options[:default] = answer
  end

  prompts = (@__prompts ||= [])

  options[:prompts] = prompts

  ans = super

  if rewind
    prompts << [prompt, options[:forget] ? nil : ans]
  end

  ans
end
finalize() click to toggle source

Clear prompts.

Questions asked after this are rewindable, but questions asked beforehand are no longer reachable.

Use this after you’ve performed some mutation based on the user’s input.

# File lib/interact/rewindable.rb, line 71
def finalize
  @__prompts = []
end
handler(which, state) click to toggle source
# File lib/interact/rewindable.rb, line 75
def handler(which, state)
  prompts = state.options[:prompts] || []

  case which
  when :up, :shift_tab
    if back = prompts.pop
      raise JumpToPrompt, back
    end
  end

  super
end
with_char_io(input) click to toggle source
# File lib/interact/rewindable.rb, line 88
def with_char_io(input)
  before = set_input_state(input)
  yield
rescue JumpToPrompt => e
  restore_input_state(input, before)
  e.jump
ensure
  restore_input_state(input, before)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.