Class: RuboCounsel::ConfigurableAttribute
- Inherits:
-
Object
- Object
- RuboCounsel::ConfigurableAttribute
- Defined in:
- lib/rubocounsel/configurable_attribute.rb
Overview
Represents one configurable aspect of a cop (e.g., EnforcedStyle, Max).
Use the .for factory to get the right subclass based on the value type:
ConfigurableAttribute.for("EnforcedStyle", "single_quotes", options: [...])
# => ChoiceAttribute
Each subclass knows how to prompt for its own value via #prompt.
Direct Known Subclasses
ArrayAttribute, BooleanAttribute, ChoiceAttribute, ScalarAttribute
Instance Attribute Summary collapse
-
#default_value ⇒ Object
readonly
Returns the value of attribute default_value.
-
#examples ⇒ Object
readonly
Returns the value of attribute examples.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #choice? ⇒ Boolean
- #has_examples? ⇒ Boolean
-
#initialize(name, default_value, options: nil, examples: []) ⇒ ConfigurableAttribute
constructor
A new instance of ConfigurableAttribute.
-
#prompt(output: $stdout) ⇒ Object
Template method: prompt with "Show examples" re-prompt loop.
- #promptable? ⇒ Boolean
Constructor Details
#initialize(name, default_value, options: nil, examples: []) ⇒ ConfigurableAttribute
Returns a new instance of ConfigurableAttribute.
29 30 31 32 33 34 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 29 def initialize(name, default_value, options: nil, examples: []) @name = name @default_value = default_value @options = @examples = examples end |
Instance Attribute Details
#default_value ⇒ Object (readonly)
Returns the value of attribute default_value.
15 16 17 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 15 def default_value @default_value end |
#examples ⇒ Object (readonly)
Returns the value of attribute examples.
15 16 17 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 15 def examples @examples end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 15 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 15 def @options end |
Class Method Details
.for(name, value, options: nil, examples: []) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 17 def self.for(name, value, options: nil, examples: []) if ChoiceAttribute.new(name, value, options: , examples: examples) elsif value.is_a?(Array) ArrayAttribute.new(name, value, examples: examples) elsif value.in?([true, false]) BooleanAttribute.new(name, value, examples: examples) else ScalarAttribute.new(name, value, examples: examples) end end |
Instance Method Details
#boolean? ⇒ Boolean
38 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 38 def boolean? = false |
#choice? ⇒ Boolean
37 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 37 def choice? = false |
#has_examples? ⇒ Boolean
40 41 42 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 40 def has_examples? @examples.any? end |
#prompt(output: $stdout) ⇒ Object
Template method: prompt with "Show examples" re-prompt loop. Subclasses provide #base_options and #parse_answer.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 46 def prompt(output: $stdout) output.puts ::CLI::UI.fmt("{{italic:No examples available for #{@name}.}}") unless has_examples? loop do = has_examples? ? + ["Show examples"] : answer = ::CLI::UI::Prompt.ask("#{@name}?", options: ) if answer == "Show examples" display_examples(output) else return parse_answer(answer) end end end |
#promptable? ⇒ Boolean
36 |
# File 'lib/rubocounsel/configurable_attribute.rb', line 36 def promptable? = true |