Sha256: 2349cccb5ef416259797c6f4534cda8c21a9925142dcb6a1689e326c1d42e4f3

Contents?: true

Size: 1.55 KB

Versions: 3

Compression:

Stored size: 1.55 KB

Contents

module Jax
  module Generators
    module Interactions
      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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jax-1.1.1 lib/jax/generators/interactions.rb
jax-1.1.0 lib/jax/generators/interactions.rb
jax-1.1.0.rc1 lib/jax/generators/interactions.rb