Class: TTY::MyPrompt

Inherits:
Prompt
  • Object
show all
Defined in:
lib/clir/TTY-Prompt.cls.rb

Constant Summary collapse

MARKER_TTY_FILE =
File.expand_path(File.join('.','.TTY_MARKER_FILE'))

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.modeObject

Returns the value of attribute mode.



30
31
32
# File 'lib/clir/TTY-Prompt.cls.rb', line 30

def mode
  @mode
end

Class Method Details

.set_mode_interactiveObject

Methods to switch hardly in interactive mode during tests



33
34
35
36
37
38
# File 'lib/clir/TTY-Prompt.cls.rb', line 33

def set_mode_interactive
  Object.send(:remove_const, 'Q')
  Object.const_set('Q', new)
  Q.init(mode_interactive = true)
  File.write(MARKER_TTY_FILE,"#{Time.now}::true")
end

.unset_mode_interactiveObject Also known as: set_mode_inputs



39
40
41
42
43
44
# File 'lib/clir/TTY-Prompt.cls.rb', line 39

def unset_mode_interactive
  Object.send(:remove_const, 'Q')
  Object.const_set('Q', new)
  File.delete(MARKER_TTY_FILE) if File.exist?(MARKER_TTY_FILE)
  Q.init(mode_interactive = false)
end

Instance Method Details

#default_name_for_value(choices_list, default_value) ⇒ Object

Sadly, for select, Tty-prompt requires the :name value for the default value (:default) in methods. This method can return :name valeur from :value

Parameters:

  • choices_list (Array)

    Choices list for select method

  • default_value (Any)

    The default value

Returns:

  • nil or :name of the choice.



100
101
102
103
104
105
106
107
# File 'lib/clir/TTY-Prompt.cls.rb', line 100

def default_name_for_value(choices_list, default_value)
  choices_list.each do |dchoix|
    if dchoix[:value] == default_value
      return dchoix[:name]
    end
  end
  return nil
end

#include_methods_by_mode(interactive_mode) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/clir/TTY-Prompt.cls.rb', line 74

def include_methods_by_mode(interactive_mode)
  if interactive_mode
    # 
    # Usual methods
    # (overwrite tests method if any)
    # 
    self.extend ReplayedTTYMethods
  else
    # 
    # Use Inputs methods instead of usual methods
    # (overwrite them)
    # 
    self.extend InputsTTYMethods
  end
  @mode = interactive_mode ? :interactive : :inputs
  self.class.mode = @mode
end

#init(mode_interactive = nil) ⇒ Object

Init Q instance



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/clir/TTY-Prompt.cls.rb', line 55

def init(mode_interactive = nil)
  if File.exist?(MARKER_TTY_FILE)
    _, mode_interactive = File.read(MARKER_TTY_FILE).split('::')
    mode_interactive = eval(mode_interactive)
    include_methods_by_mode(mode_interactive)
  elsif mode_interactive === nil 
    toggle_mode
  else
    include_methods_by_mode(mode_interactive)
  end
  @inputs = nil # for testing
end

#toggle_modeObject

Méthode qui fait basculer du mode normal au mode test et inversement.



70
71
72
# File 'lib/clir/TTY-Prompt.cls.rb', line 70

def toggle_mode
  include_methods_by_mode(not(CLI::Replayer.on? || test? ))
end