Module: Jax::Generators::Actions

Included in:
PluginGenerator, PluginManager
Defined in:
lib/generators/jax/base/actions.rb

Instance Method Summary collapse

Instance Method Details



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/generators/jax/base/actions.rb', line 7

def menu(items, options = {})
  min = 1
  if options[:allow_all]
    say_option 0, "All candidates"
    min = 0
  end
  
  items.each_with_index do |item, index|
    say_option index+1, item
  end
  which = menu_choice(:min => min, :max => items.length)
                  
  if which == -1
    items.each_with_index { |item, index| yield item, index }
  else
    yield items[which], which
  end
end


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/jax/base/actions.rb', line 26

def menu_choice(*args)
  options = args.extract_options!
  caption, addl_caption = *args
  caption = "Please select an option, or press ctrl+c to cancel >" unless caption
  
  which = ask("#{addl_caption}#{caption}")
  sel = which.to_i
  # if sel.to_s != which then which is non-numeric
  if sel.to_s != which || options[:min] && sel < options[:min] || options[:max] && sel > options[:max]
    menu_choice(caption, "Invalid choice. ")
  else
    sel - 1
  end
end

#overwrite(path) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/generators/jax/base/actions.rb', line 45

def overwrite(path)
  path = path.to_s
  if File.exist? path
    prompt_yn "Path '#{path}' already exists! Delete it?"
    FileUtils.rm_rf path
  end
end

#prompt_yn(message, options = {}) ⇒ Object



2
3
4
5
# File 'lib/generators/jax/base/actions.rb', line 2

def prompt_yn(message, options = {})
  yn = ask(message).downcase[0]
  throw :aborted, "Aborted by user." if yn != ?y
end

#say_option(which, caption) ⇒ Object



41
42
43
# File 'lib/generators/jax/base/actions.rb', line 41

def say_option(which, caption)
  say "\t#{which}\t: #{caption}"
end