Class: Ibex::Configuration::Key
- Inherits:
-
Object
- Object
- Ibex::Configuration::Key
- Defined in:
- lib/ibex/configuration.rb
Overview
One closed definition of a configuration concept and its value domain.
Instance Attribute Summary collapse
-
#allowed_values ⇒ Object
readonly
: Array?.
-
#cli_aliases ⇒ Object
readonly
: Array.
-
#cli_option ⇒ Object
readonly
: Symbol?.
-
#default ⇒ Object
readonly
: config_value.
-
#name ⇒ Object
readonly
: String.
-
#owner ⇒ Object
readonly
: Symbol.
-
#parser_setting ⇒ Object
readonly
: Symbol?.
-
#type ⇒ Object
readonly
: Symbol.
Instance Method Summary collapse
-
#initialize(name, type:, default:, owner:, allowed_values: nil, cli_option: nil, parser_setting: nil, cli_aliases: []) ⇒ Key
constructor
A new instance of Key.
- #owner_name ⇒ Object
- #policy ⇒ Object
-
#validate(value) ⇒ Object
Validate and defensively freeze a value from an adapter or declaration.
Constructor Details
#initialize(name, type:, default:, owner:, allowed_values: nil, cli_option: nil, parser_setting: nil, cli_aliases: []) ⇒ Key
Returns a new instance of Key.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ibex/configuration.rb', line 48 def initialize(name, type:, default:, owner:, allowed_values: nil, cli_option: nil, parser_setting: nil, cli_aliases: []) validate_identity!(name, owner) @name = name.dup.freeze @type = type @owner = owner validate_shape!(type, cli_option) @allowed_values = allowed_values&.map { |value| immutable(value) }&.freeze @cli_option = cli_option @parser_setting = parser_setting&.to_sym @cli_aliases = cli_aliases.map(&:to_s).freeze @default = validate(default) freeze end |
Instance Attribute Details
#allowed_values ⇒ Object (readonly)
: Array?
40 41 42 |
# File 'lib/ibex/configuration.rb', line 40 def allowed_values @allowed_values end |
#cli_aliases ⇒ Object (readonly)
: Array
43 44 45 |
# File 'lib/ibex/configuration.rb', line 43 def cli_aliases @cli_aliases end |
#cli_option ⇒ Object (readonly)
: Symbol?
41 42 43 |
# File 'lib/ibex/configuration.rb', line 41 def cli_option @cli_option end |
#default ⇒ Object (readonly)
: config_value
38 39 40 |
# File 'lib/ibex/configuration.rb', line 38 def default @default end |
#name ⇒ Object (readonly)
: String
36 37 38 |
# File 'lib/ibex/configuration.rb', line 36 def name @name end |
#owner ⇒ Object (readonly)
: Symbol
39 40 41 |
# File 'lib/ibex/configuration.rb', line 39 def owner @owner end |
#parser_setting ⇒ Object (readonly)
: Symbol?
42 43 44 |
# File 'lib/ibex/configuration.rb', line 42 def parser_setting @parser_setting end |
#type ⇒ Object (readonly)
: Symbol
37 38 39 |
# File 'lib/ibex/configuration.rb', line 37 def type @type end |
Instance Method Details
#owner_name ⇒ Object
78 79 80 |
# File 'lib/ibex/configuration.rb', line 78 def owner_name OWNER_NAMES.fetch(@owner) end |
#policy ⇒ Object
83 84 85 |
# File 'lib/ibex/configuration.rb', line 83 def policy OWNER_POLICIES.fetch(@owner) end |
#validate(value) ⇒ Object
Validate and defensively freeze a value from an adapter or declaration.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ibex/configuration.rb', line 66 def validate(value) raise ArgumentError, "#{@name} expects #{@type}, got #{value.inspect}" unless valid_type?(value) allowed_values = @allowed_values if allowed_values && !allowed_values.include?(value) raise ArgumentError, "#{@name} must be one of #{allowed_values.map(&:inspect).join(', ')}" end immutable(value) end |