Class: KafoWizards::HighLine::Wizard

Inherits:
AbstractWizard show all
Defined in:
lib/kafo_wizards/highline/wizard.rb

Instance Attribute Summary

Attributes inherited from AbstractWizard

#description, #entries, #header, #interactive, #logger, #renderers, #validators

Instance Method Summary collapse

Methods inherited from AbstractWizard

#buttons, default_renderers, #factory, #initialize, register_default_renderer, #run, #triggers, #update, #validate, #values

Constructor Details

This class inherits a constructor from KafoWizards::AbstractWizard

Instance Method Details

#execute_menuObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kafo_wizards/highline/wizard.rb', line 7

def execute_menu
  begin
    choice = nil
    loop do
      say "\n"
      say ::HighLine.color(header, :yellow) unless header.empty?
      print_values
      say "\n" + description
      choice = print_menu
      break unless choice.nil?
    end
    validate(values) if triggers.include? choice
    choice
  rescue KafoWizards::ValidationError => e
    say ::HighLine.color("\nUnable to procedd due to following error(s):", :red)
    say ::HighLine.color(format_errors(e.messages), :red)
    say "\n"
    retry
  end
end


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/kafo_wizards/highline/wizard.rb', line 41

def print_menu
  choose do |menu|
    menu.header = ::HighLine.color("\nAvailable actions", :white)
    menu.prompt = 'Your choice: '
    menu.select_by = :index

    sorted_entries.each do |entry|
      menu.choice(render_entry(entry)) do
        begin
          entry.call_pre_hook
          res = render_action(entry)
          entry.call_post_hook
          res
        rescue KafoWizards::ValidationError => e
          say ::HighLine.color("\nValidation error: #{e.message}", :red)
          say "Please, try again... \n\n"
          retry
        rescue Interrupt
          nil
        end
      end
    end
  end
end


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kafo_wizards/highline/wizard.rb', line 28

def print_values
  entries.each do |entry|
    max_label_width = 25
    next if (entry.class <= KafoWizards::Entries::ButtonEntry)
    if entry.required?
      label = ::HighLine.color(('*'+entry.label).rjust(max_label_width), :bold, :yellow)
    else
      label = entry.label.rjust(max_label_width)
    end
    say "#{label}: " + render_value(entry)
  end
end

#render_action(entry) ⇒ Object



67
68
69
# File 'lib/kafo_wizards/highline/wizard.rb', line 67

def render_action(entry)
  call_renderer_for_entry(:render_action, entry)
end

#render_entry(entry) ⇒ Object



75
76
77
# File 'lib/kafo_wizards/highline/wizard.rb', line 75

def render_entry(entry)
  call_renderer_for_entry(:render_entry, entry)
end

#render_value(entry) ⇒ Object



71
72
73
# File 'lib/kafo_wizards/highline/wizard.rb', line 71

def render_value(entry)
  call_renderer_for_entry(:render_value, entry)
end