Class: RuboCounsel::ConfigurableAttribute

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = options
  @examples = examples
end

Instance Attribute Details

#default_valueObject (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

#examplesObject (readonly)

Returns the value of attribute examples.



15
16
17
# File 'lib/rubocounsel/configurable_attribute.rb', line 15

def examples
  @examples
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/rubocounsel/configurable_attribute.rb', line 15

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/rubocounsel/configurable_attribute.rb', line 15

def options
  @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 options
    ChoiceAttribute.new(name, value, options: 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

Returns:

  • (Boolean)


38
# File 'lib/rubocounsel/configurable_attribute.rb', line 38

def boolean? = false

#choice?Boolean

Returns:

  • (Boolean)


37
# File 'lib/rubocounsel/configurable_attribute.rb', line 37

def choice? = false

#has_examples?Boolean

Returns:

  • (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
    options = has_examples? ? base_options + ["Show examples"] : base_options
    answer = ::CLI::UI::Prompt.ask("#{@name}?", options: options)

    if answer == "Show examples"
      display_examples(output)
    else
      return parse_answer(answer)
    end
  end
end

#promptable?Boolean

Returns:

  • (Boolean)


36
# File 'lib/rubocounsel/configurable_attribute.rb', line 36

def promptable? = true