Module: A2::Approved

Instance Method Summary collapse

Instance Method Details

#add_approval_option!(options, opt = {}) ⇒ Object



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

def add_approval_option!(options, opt = {})
  options.on('-y', '--yes', 'Auto approve the deletion prompt.') do
    opt[:auto_approved] = true
  end
  opt
end

#ask_for_approval(message = '') ⇒ Object



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

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

#with_approval(opts, &block) ⇒ Object



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

def with_approval(opts, &block)
  opts[:auto_approved] = opts[:auto_approved] || false
  answer = 'yes'
  answer = ask_for_approval(opts[:message]) unless opts[:auto_approved]
  block.call if answer.eql?('yes')
end