Class: CLIUtils::Pref
- Inherits:
-
Object
- Object
- CLIUtils::Pref
- 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.
[ { class_suffix: 'Action', file_suffix: 'action' }, { class_suffix: 'Behavior', file_suffix: 'behavior' }, { class_suffix: 'Validator', file_suffix: 'validator' } ]
Instance Attribute Summary collapse
-
#answer ⇒ String, Symbol
Stores the answer to this Pref.
-
#behavior_objects ⇒ Array
Stores instantiated Behavior objects.
-
#behaviors ⇒ Array
Stores the behaviors that this Pref conforms to.
-
#config_key ⇒ String, Symbol
Stores a key to reference this pref in a Configurator.
-
#config_section ⇒ String, Symbol
Stores a Configurator section to stick this Pref under.
-
#default ⇒ String
Stores the default text.
-
#last_error_message ⇒ String
Stores the last error message.
-
#options ⇒ Array
Stores the valid options the user can pick from.
-
#post ⇒ Hash
Stores the message and behavior that should be executed after the prompt is delivered.
-
#pre ⇒ Hash
Stores the message and behavior that should be executed before the prompt is delivered.
-
#prereqs ⇒ Array
Stores the prereqs information.
-
#prompt_text ⇒ String
Stores the prompt text.
-
#validator_objects ⇒ Array
Stores instantiated Validators.
-
#validators ⇒ Hash
Stores key/value combinations required to show this Pref.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Custom equality operator for this class.
-
#deliver(default = nil) ⇒ void
Delivers the prompt the user.
-
#evaluate_behaviors(text) ⇒ String
Runs the passed text through this Pref’s behaviors.
-
#initialize(params = {}) ⇒ void
constructor
Initializes a new Pref via passed-in parameters.
-
#validate(text) ⇒ Boolean
Validates a text against this pref’s options and validators.
Methods included from Messaging
Constructor Details
#initialize(params = {}) ⇒ void
Initializes a new Pref via passed-in parameters. Also initializes objects for each Validator and Behavior on this Pref.
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
#answer ⇒ String, Symbol
Stores the answer to this Pref.
27 28 29 |
# File 'lib/cliutils/prefs/pref.rb', line 27 def answer @answer end |
#behavior_objects ⇒ Array
Stores instantiated Behavior objects.
31 32 33 |
# File 'lib/cliutils/prefs/pref.rb', line 31 def behavior_objects @behavior_objects end |
#behaviors ⇒ Array
Stores the behaviors that this Pref conforms to.
35 36 37 |
# File 'lib/cliutils/prefs/pref.rb', line 35 def behaviors @behaviors end |
#config_key ⇒ String, Symbol
Stores a key to reference this pref in a Configurator.
39 40 41 |
# File 'lib/cliutils/prefs/pref.rb', line 39 def config_key @config_key end |
#config_section ⇒ String, Symbol
Stores a Configurator section to stick this Pref under.
43 44 45 |
# File 'lib/cliutils/prefs/pref.rb', line 43 def config_section @config_section end |
#default ⇒ String
Stores the default text.
47 48 49 |
# File 'lib/cliutils/prefs/pref.rb', line 47 def default @default end |
#last_error_message ⇒ String
Stores the last error message.
51 52 53 |
# File 'lib/cliutils/prefs/pref.rb', line 51 def @last_error_message end |
#options ⇒ Array
Stores the valid options the user can pick from.
55 56 57 |
# File 'lib/cliutils/prefs/pref.rb', line 55 def @options end |
#post ⇒ Hash
Stores the message and behavior that should be executed after the prompt is delivered.
60 61 62 |
# File 'lib/cliutils/prefs/pref.rb', line 60 def post @post end |
#pre ⇒ Hash
Stores the message and behavior that should be executed before the prompt is delivered.
65 66 67 |
# File 'lib/cliutils/prefs/pref.rb', line 65 def pre @pre end |
#prereqs ⇒ Array
Stores the prereqs information.
69 70 71 |
# File 'lib/cliutils/prefs/pref.rb', line 69 def prereqs @prereqs end |
#prompt_text ⇒ String
Stores the prompt text.
73 74 75 |
# File 'lib/cliutils/prefs/pref.rb', line 73 def prompt_text @prompt_text end |
#validator_objects ⇒ Array
Stores instantiated Validators
77 78 79 |
# File 'lib/cliutils/prefs/pref.rb', line 77 def validator_objects @validator_objects end |
#validators ⇒ Hash
Stores key/value combinations required to show this Pref.
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.
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.
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.
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.
151 152 153 154 |
# File 'lib/cliutils/prefs/pref.rb', line 151 def validate(text) (text) && _check_validators(text) end |