Module: A2::Approved

Instance Method Summary collapse

Instance Method Details

#ask_for_approval(message) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/a2/mixins/approved.rb', line 11

def ask_for_approval(message)
  puts "Are you sure you want to #{message}?"
  puts "Only 'yes' will be accepted to proceed:"
  answer = $stdin.gets.chomp
  abort('Operation cancelled') unless answer.eql?('yes')
  answer
end

#initialize(name, opts = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/a2/mixins/approved.rb', line 3

def initialize(name, opts = {})
  super(name, takes_commands: false)
  @opt = {}
  options.on('-y', '--yes', 'Auto approve the deletion prompt.') do
    @opt[:yes] = true
  end
end

#with_approval(message, &block) ⇒ Object



19
20
21
22
23
# File 'lib/a2/mixins/approved.rb', line 19

def with_approval(message, &block)
  answer = 'yes'
  answer = ask_for_approval(message) unless @opt[:yes]
  block.call if answer.eql?('yes')
end