Class: Highline::Wizard::Wizzard

Inherits:
Object
  • Object
show all
Defined in:
lib/highline/wizard.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) {|@options| ... } ⇒ Wizzard

Returns a new instance of Wizzard.

Yields:

  • (@options)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/highline/wizard.rb', line 8

def initialize(data = nil)
  @options = Struct.new(:titles, :defaults, :data).new
  @options.defaults = []
  @options.titles = []
  @options.data ||= data
  @options.data.freeze
  @hl = HighLine.new
  yield @options if block_given?

  result = []
  current_data = data.dup
  columns_count.times do |column_index|
    values = current_data.transpose[column_index].uniq
    choosen = nil
    if values.size > 1
      elected = choose(values, @options.titles[column_index], @options.defaults[column_index])
    else
      elected = values.first
    end

    result << elected

    current_data = current_data.select{|row|
      row[column_index] == elected
    }

  end
  @selected = result
end

Instance Attribute Details

#selectedObject (readonly)

Returns the value of attribute selected.



7
8
9
# File 'lib/highline/wizard.rb', line 7

def selected
  @selected
end

Instance Method Details

#choose(values, title, default = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/highline/wizard.rb', line 38

def choose(values, title, default = nil)
  @hl.choose(*(values.sort)) do  |menu|
    default = nil unless values.include?(default)
    prompt = "Please choose"
    prompt << " #{title}" if title
    prompt << " ( Default: #{default} )" if default
    menu.prompt = prompt
    menu.default = default if default
  end
end