Class: CodeownerValidator::ConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowner_validator/helpers/config_helper.rb

Overview

Public: A configuration helper designed to assist in simple abstraction of TTY:Prompt requests and assign those responses to the base module’s configuration

Class Method Summary collapse

Class Method Details

.ask(ident:, prompt:, required: false, force_ask: false, mask: false, **args) ⇒ Object

Public: An abstraction onto the TTY:Prompt.ask to allow requesting information from the user’s input and saving that input within the base module’s configuration

Parameters:

  • args (Hash)

    The arguments in which to accept

Options Hash (**args):

  • :ident (String)

    The identifier to the config to store

  • :prompt (String)

    The prompt to display to the user

  • :default (String)

    The default value to display

  • :required (Boolean)

    Indicates if the requested ask required input from the user

  • :force_ask (Boolean)

    Indicates to ignore previous ask and perform again

  • :mask (Boolean)

    Indicates that the response should be hidden



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/codeowner_validator/helpers/config_helper.rb', line 21

def ask(ident:, prompt:, required: false, force_ask: false, mask: false, **args)
  opts =
    {}.tap do |h|
      h[:required] = required
      h[:default] = args[:default] if args[:default]
    end

  # return if either not being forced to ask or the information has been previously captured
  return if !force_ask && ::CodeownerValidator.respond_to?(ident)

  tty_prompt = ::TTY::Prompt.new
  response =
    tty_prompt.collect do
      if mask
        key(ident.to_sym).mask(
          prompt,
          opts
        )
      else
        key(ident.to_sym).ask(
          prompt,
          opts
        )
      end
    end

  ::CodeownerValidator.configure! response
end

.select(ident:, prompt:, force_ask: false, choices:, **args) ⇒ Object

Public: An abstraction onto the TTY:Prompt.select to allow requesting information from the user’s input and saving that input within the base module’s configuration

Parameters:

  • args (Hash)

    The arguments in which to accept

Options Hash (**args):

  • :ident (String)

    The identifier to the config to store

  • :prompt (String)

    The prompt to display to the user

  • :force_ask (Boolean)

    Indicates to ignore previous ask and perform again

  • :choices (Boolean)

    The array of choices available for selection by the user



58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/codeowner_validator/helpers/config_helper.rb', line 58

def select(ident:, prompt:, force_ask: false, choices:, **args)
  # return if either not being forced to ask or the information has been previously captured
  return if !force_ask && ::CodeownerValidator.respond_to?(ident)

  tty_prompt = ::TTY::Prompt.new
  response = tty_prompt.select(
    prompt,
    choices,
    args
  )

  ::CodeownerValidator.configure! ident => response
end