Sha256: 37a1e0dc8dac0415289e3653d8c542771b4951bd2f59c7a9465c51b6fc0d285f

Contents?: true

Size: 1.32 KB

Versions: 15

Compression:

Stored size: 1.32 KB

Contents

module Jax::Generators::Actions
  def prompt_yn(message, options = {})
    yn = ask(message).downcase[0]
    throw :aborted, "Aborted by user." if yn != ?y
  end
  
  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
  
  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
  
  def say_option(which, caption)
    say "\t#{which}\t: #{caption}"
  end
  
  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
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jax-3.0.0.rc2 lib/generators/jax/base/actions.rb
jax-2.0.12 lib/generators/jax/base/actions.rb
jax-3.0.0.rc1 lib/generators/jax/base/actions.rb
jax-2.0.11 lib/generators/jax/base/actions.rb
jax-2.0.10 lib/generators/jax/base/actions.rb
jax-2.0.9 lib/generators/jax/base/actions.rb
jax-2.0.8 lib/generators/jax/base/actions.rb
jax-2.0.7 lib/generators/jax/base/actions.rb
jax-2.0.6 lib/generators/jax/base/actions.rb
jax-2.0.5 lib/generators/jax/base/actions.rb
jax-2.0.4 lib/generators/jax/base/actions.rb
jax-2.0.3 lib/generators/jax/base/actions.rb
jax-2.0.2 lib/generators/jax/base/actions.rb
jax-2.0.1 lib/generators/jax/base/actions.rb
jax-2.0.0 lib/generators/jax/base/actions.rb