Class: CLIUtils::Pref

Inherits:
Object
  • Object
show all
Includes:
Messaging
Defined in:
lib/cliutils/prefs/pref.rb

Overview

Pref Class An individual preference

Constant Summary collapse

ASSET_TYPE_ACTION =

Constant defining an Action

0
ASSET_TYPE_BEHAVIOR =

Constant defining a Behavior

1
ASSET_TYPE_VALIDATOR =

Constant defining a Validator

2
@@asset_labels =

Contains a listing of asset names for classes and file suffixes.

Returns:

  • (Array)
[
  { class_suffix: 'Action',    file_suffix: 'action'    },
  { class_suffix: 'Behavior',  file_suffix: 'behavior'  },
  { class_suffix: 'Validator', file_suffix: 'validator' }
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Messaging

#messenger

Constructor Details

#initialize(params = {}) ⇒ void

Initializes a new Pref via passed-in parameters. Also initializes objects for each Validator and Behavior on this Pref.

Parameters:

  • params (Hash) (defaults to: {})

    Parameters to initialize



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cliutils/prefs/pref.rb', line 88

def initialize(params = {})
  @behavior_objects = []
  @validator_objects = []

  # Assign all of the passed params as instance variables.
  params.each { |key, value| send("#{ key }=", value) }

  # Instantiate any listed Behaviors or Validators.
  @behaviors.each { |b| _init_and_add_behavior(b) } if @behaviors
  @validators.each { |v| _init_and_add_validator(v) } if @validators
end

Instance Attribute Details

#answerString, Symbol

Stores the answer to this Pref.

Returns:



27
28
29
# File 'lib/cliutils/prefs/pref.rb', line 27

def answer
  @answer
end

#behavior_objectsArray

Stores instantiated Behavior objects.

Returns:

  • (Array)


31
32
33
# File 'lib/cliutils/prefs/pref.rb', line 31

def behavior_objects
  @behavior_objects
end

#behaviorsArray

Stores the behaviors that this Pref conforms to.

Returns:

  • (Array)


35
36
37
# File 'lib/cliutils/prefs/pref.rb', line 35

def behaviors
  @behaviors
end

#config_keyString, Symbol

Stores a key to reference this pref in a Configurator.

Returns:



39
40
41
# File 'lib/cliutils/prefs/pref.rb', line 39

def config_key
  @config_key
end

#config_sectionString, Symbol

Stores a Configurator section to stick this Pref under.

Returns:



43
44
45
# File 'lib/cliutils/prefs/pref.rb', line 43

def config_section
  @config_section
end

#defaultString

Stores the default text.

Returns:



47
48
49
# File 'lib/cliutils/prefs/pref.rb', line 47

def default
  @default
end

#last_error_messageString

Stores the last error message.

Returns:



51
52
53
# File 'lib/cliutils/prefs/pref.rb', line 51

def last_error_message
  @last_error_message
end

#optionsArray

Stores the valid options the user can pick from.

Returns:

  • (Array)


55
56
57
# File 'lib/cliutils/prefs/pref.rb', line 55

def options
  @options
end

#postHash

Stores the message and behavior that should be executed after the prompt is delivered.

Returns:



60
61
62
# File 'lib/cliutils/prefs/pref.rb', line 60

def post
  @post
end

#preHash

Stores the message and behavior that should be executed before the prompt is delivered.

Returns:



65
66
67
# File 'lib/cliutils/prefs/pref.rb', line 65

def pre
  @pre
end

#prereqsArray

Stores the prereqs information.

Returns:

  • (Array)


69
70
71
# File 'lib/cliutils/prefs/pref.rb', line 69

def prereqs
  @prereqs
end

#prompt_textString

Stores the prompt text.

Returns:



73
74
75
# File 'lib/cliutils/prefs/pref.rb', line 73

def prompt_text
  @prompt_text
end

#validator_objectsArray

Stores instantiated Validators

Returns:

  • (Array)


77
78
79
# File 'lib/cliutils/prefs/pref.rb', line 77

def validator_objects
  @validator_objects
end

#validatorsHash

Stores key/value combinations required to show this Pref.

Returns:



81
82
83
# File 'lib/cliutils/prefs/pref.rb', line 81

def validators
  @validators
end

Instance Method Details

#==(other) ⇒ Boolean

Custom equality operator for this class.

Parameters:

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/cliutils/prefs/pref.rb', line 103

def ==(other)
  @config_key == other.config_key &&
  @config_section == other.config_section &&
  @prompt_text == other.prompt_text
end

#deliver(default = nil) ⇒ void

This method returns an undefined value.

Delivers the prompt the user. Handles retries after incorrect answers, validation, behavior evaluation, and pre-/post-behaviors.

Parameters:

  • default (String) (defaults to: nil)

    The default option



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cliutils/prefs/pref.rb', line 114

def deliver(default = nil)
  # Design decision: the pre-prompt behavior
  # gets evaluated *once*, not every time the
  # user gets prompted. This prevents multiple
  # evaluations when bad options are provided.
  _eval_pre if @pre

  valid_option_chosen = false
  until valid_option_chosen
    response = messenger.prompt(@prompt_text, default)
    if validate(response)
      valid_option_chosen = true
      @answer = evaluate_behaviors(response)
      _eval_post if @post
    else
      messenger.error(@last_error_message)
    end
  end
end

#evaluate_behaviors(text) ⇒ String

Runs the passed text through this Pref’s behaviors.

Parameters:

  • text (String)

    The text to evaluate

Returns:



137
138
139
140
141
142
143
144
145
# File 'lib/cliutils/prefs/pref.rb', line 137

def evaluate_behaviors(text)
  modified_text = text
  if @behavior_objects
    @behavior_objects.each do |b|
      modified_text = b.evaluate(modified_text)
    end
  end
  modified_text
end

#validate(text) ⇒ Boolean

Validates a text against this pref’s options and validators.

Parameters:

  • text (String)

    The text to validate

Returns:

  • (Boolean)


151
152
153
154
# File 'lib/cliutils/prefs/pref.rb', line 151

def validate(text)
  _check_options(text) &&
  _check_validators(text)
end