Class: ConsoleThinkTwice::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/console_think_twice/configuration.rb

Constant Summary collapse

DISABLING_ENV_VALUES =
%w[0 false no off].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



12
13
14
# File 'lib/console_think_twice/configuration.rb', line 12

def initialize
  @affirmative_answers = %w[y yes]
end

Instance Attribute Details

#affirmative_answersObject

Answers accepted as a confirmation; anything else aborts.



10
11
12
# File 'lib/console_think_twice/configuration.rb', line 10

def affirmative_answers
  @affirmative_answers
end

#enabled=(value) ⇒ Object (writeonly)

Sets the attribute enabled

Parameters:

  • value

    the value to set the attribute enabled to.



7
8
9
# File 'lib/console_think_twice/configuration.rb', line 7

def enabled=(value)
  @enabled = value
end

#inputObject



16
17
18
# File 'lib/console_think_twice/configuration.rb', line 16

def input
  @input || $stdin
end

#interactive=(value) ⇒ Object (writeonly)

Sets the attribute interactive

Parameters:

  • value

    the value to set the attribute interactive to.



7
8
9
# File 'lib/console_think_twice/configuration.rb', line 7

def interactive=(value)
  @interactive = value
end

#labelObject

Shown in the prompt to make the stakes obvious, e.g. "in production". Defaults to the Rails environment; assigning any value at all — nil and false included — wins over that, so the label can be turned off as well as changed.



41
42
43
44
45
# File 'lib/console_think_twice/configuration.rb', line 41

def label
  return @label if defined?(@label)

  ::Rails.env.to_s if defined?(::Rails) && ::Rails.respond_to?(:env)
end

#outputObject



20
21
22
# File 'lib/console_think_twice/configuration.rb', line 20

def output
  @output || $stdout
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/console_think_twice/configuration.rb', line 24

def enabled?
  return @enabled unless @enabled.nil?

  !DISABLING_ENV_VALUES.include?(ENV["CONSOLE_THINK_TWICE"].to_s.strip.downcase)
end

#interactive?Boolean

Whether there is a human on the other end who can answer the prompt. Auto-detected from the input stream unless set, so piped input aborts instead of destroying unattended.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/console_think_twice/configuration.rb', line 32

def interactive?
  return @interactive unless @interactive.nil?

  input.respond_to?(:tty?) && input.tty?
end