Class: SlackProgressBar::Config
- Inherits:
-
Object
- Object
- SlackProgressBar::Config
- Defined in:
- lib/slack_progress_bar/config.rb
Constant Summary collapse
- InvalidConfigError =
Class.new(StandardError)
- DEFAULT_PREFIX =
"pb"
- DEFAULT_LETTERS =
The default letters stand for: purple, blue, green, yellow, orange, red, and white. See SlackProgressBar::Generator::DEFAULT_COLORS for more information.
%w[p b g y o r w].freeze
- DEFAULT_ALIASES =
{ purple: "p", blue: "b", green: "g", yellow: "y", orange: "o", red: "r", white: "w", }.freeze
- PREFIX_PATTERN =
/\A[0-9a-z]+\z/
- LETTER_PATTERN =
/\A[a-z]\z/
- SEPARATOR =
"-"
- LEFT_CAP_SUFFIX =
"a"
- RIGHT_CAP_SUFFIX =
"z"
- CIRCLE_SUFFIX =
"o"
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#letters ⇒ Object
Returns the value of attribute letters.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Instance Method Summary collapse
- #circle_suffix ⇒ Object
- #default_letter ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #left_cap_suffix ⇒ Object
- #right_cap_suffix ⇒ Object
- #separator ⇒ Object
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
34 35 36 37 38 |
# File 'lib/slack_progress_bar/config.rb', line 34 def initialize @prefix = DEFAULT_PREFIX @letters = DEFAULT_LETTERS @aliases = DEFAULT_ALIASES end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
32 33 34 |
# File 'lib/slack_progress_bar/config.rb', line 32 def aliases @aliases end |
#letters ⇒ Object
Returns the value of attribute letters.
32 33 34 |
# File 'lib/slack_progress_bar/config.rb', line 32 def letters @letters end |
#prefix ⇒ Object
Returns the value of attribute prefix.
32 33 34 |
# File 'lib/slack_progress_bar/config.rb', line 32 def prefix @prefix end |
Instance Method Details
#circle_suffix ⇒ Object
117 118 119 |
# File 'lib/slack_progress_bar/config.rb', line 117 def circle_suffix CIRCLE_SUFFIX end |
#default_letter ⇒ Object
101 102 103 |
# File 'lib/slack_progress_bar/config.rb', line 101 def default_letter letters.last end |
#left_cap_suffix ⇒ Object
109 110 111 |
# File 'lib/slack_progress_bar/config.rb', line 109 def left_cap_suffix LEFT_CAP_SUFFIX end |
#right_cap_suffix ⇒ Object
113 114 115 |
# File 'lib/slack_progress_bar/config.rb', line 113 def right_cap_suffix RIGHT_CAP_SUFFIX end |
#separator ⇒ Object
105 106 107 |
# File 'lib/slack_progress_bar/config.rb', line 105 def separator SEPARATOR end |
#valid? ⇒ Boolean
93 94 95 96 97 98 99 |
# File 'lib/slack_progress_bar/config.rb', line 93 def valid? validate! rescue InvalidConfigError false else true end |
#validate! ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/slack_progress_bar/config.rb', line 40 def validate! unless prefix.is_a?(String) raise InvalidConfigError, "The prefix must be a String. Found #{prefix.class.name}: #{prefix.inspect}" end unless PREFIX_PATTERN.match?(prefix) raise InvalidConfigError, "The prefix must only contain digits 0-9 and/or lowercase letters a-z. Found: #{prefix.inspect}" end unless letters.is_a?(Array) raise InvalidConfigError, "Configured letters must be an Array. Found #{letters.class.name}: #{letters.inspect}" end case letters.size when 0 raise InvalidConfigError, "Provide at least two letters." when 1 raise InvalidConfigError, "Provide at least two letters. The last letter will be used as the default." end unless letters.uniq.size == letters.size raise InvalidConfigError, "Configured letters must be unique." end letters.each do |letter| unless letter.is_a?(String) raise InvalidConfigError, "Every letter must be a String. Found #{letter.class.name}: #{letter.inspect}" end unless LETTER_PATTERN.match?(letter) raise InvalidConfigError, "The only valid letters are lowercase a-z. Found: #{letter.inspect}" end end unless aliases.is_a?(Hash) raise InvalidConfigError, "Configured aliases must be a Hash. Found #{aliases.class.name}: #{aliases.inspect}" end aliases.each do |name, letter| unless name.is_a?(Symbol) raise InvalidConfigError, "Every alias must have a Symbol name. Found #{name.class.name}: #{name.inspect}" end unless letter.is_a?(String) raise InvalidConfigError, "Every alias must point to a String letter. Found #{letter.class.name}: #{letter.inspect}" end unless letters.include?(letter) raise InvalidConfigError, "Every alias must point to a valid letter. Found: #{letter.inspect}" end end end |