Module: Inquirer

Extended by:
Inquirer
Included in:
Inquirer, SpecHelpers, SpecHelpers::All
Defined in:
lib/inquirer.rb,
lib/inquirer/style.rb,
lib/inquirer/version.rb,
lib/inquirer/style/list.rb,
lib/inquirer/style/input.rb,
lib/inquirer/spec_helpers.rb,
lib/inquirer/style/confirm.rb,
lib/inquirer/style/checkbox.rb,
lib/inquirer/style/password.rb,
lib/inquirer/style/list_filterable.rb,
lib/inquirer/style/checkbox_filterable.rb

Defined Under Namespace

Modules: SpecHelpers Classes: Style

Constant Summary collapse

VERSION =
'0.0.9'

Instance Method Summary collapse

Instance Method Details

#prompt(prompts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/inquirer.rb', line 15

def prompt(prompts)

  answers = {}
  prompts.each { |prompt|

    # TODO: maybe use prompt.fetch(:type, :input) ???
    prompt[:type] ||= :input

    prompt_answers = []

    if prompt[:when].is_a?(Proc)

      ask_prompt = prompt[:when].call( answers )

      next if !ask_prompt
    elsif [true, false].include? prompt[:when]
      next if !prompt[:when]
    end

    if !prompt[:repeat].is_a?(Integer) and ![true, false].include? prompt[:repeat] # Boolean check :(
      prompt[:repeat] = 1
    end

    repeat_prompt = lambda { |repeat_counter|

      if prompt[:manipulate] && prompt[:manipulate].is_a?(Proc)

        manipulate_parameter = prompt.merge(
          answers:        answers,
          repeat_counter: repeat_counter,
        )

        prompt = prompt[:manipulate].call( manipulate_parameter )

        next if !prompt
      end

      type_parameter = prompt.merge(
        answers:        answers,
        repeat_counter: repeat_counter,
      )

      object = Kernel.const_get(prompt[:type].to_s.split('_').collect(&:capitalize).join)
      answer = object.prompt(type_parameter)

      if prompt[:filter] && prompt[:filter].is_a?(Proc)
        answer = prompt[:filter].call( answer )
      end

      prompt_answers.push answer
    }

    if prompt[:repeat].is_a?(Integer)
      (1..prompt[:repeat]).each(&repeat_prompt)
    else

      begin
        repeat_counter = 1
        loop {
          repeat_prompt.call(repeat_counter)
          repeat_counter += 1
        }
      rescue Interrupt
      end
    end

    if prompt[:repeat] == 1
      answers[ prompt[:name] ] = prompt_answers.first
    else
      answers[ prompt[:name] ] = prompt_answers
    end
  }

  answers
end

#use_inquirer(describe_block, opts) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/inquirer/spec_helpers.rb', line 35

def use_inquirer(describe_block, opts)
  describe_block.before opts[:with] do
    IOHelper.reset
  end

  describe_block.after opts[:with] do
    IOHelper.reset
  end
end