Class: Ibex::Configuration::Key
- Inherits:
-
Object
- Object
- Ibex::Configuration::Key
- Defined in:
- lib/ibex/configuration.rb,
sig/ibex/configuration.rbs
Overview
One closed definition of a configuration concept and its value domain.
Instance Attribute Summary collapse
- #allowed_values ⇒ Array[config_value]? readonly
- #cli_option ⇒ Symbol? readonly
- #default ⇒ config_value readonly
- #name ⇒ String readonly
- #owner ⇒ Symbol readonly
- #policy ⇒ Symbol readonly
- #type ⇒ Symbol readonly
Instance Method Summary collapse
- #immutable(value) ⇒ config_value
-
#initialize(name, type:, default:, owner:, policy:, allowed_values: nil, cli_option: nil) ⇒ Key
constructor
A new instance of Key.
- #owner_name ⇒ String
- #valid_type?(value) ⇒ Boolean
-
#validate(value) ⇒ config_value
Validate and defensively freeze a value from an adapter or declaration.
- #validate_identity!(name, owner, policy) ⇒ void
- #validate_shape!(type, policy, cli_option) ⇒ void
Constructor Details
#initialize(name, type:, default:, owner:, policy:, allowed_values: nil, cli_option: nil) ⇒ Key
Returns a new instance of Key.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ibex/configuration.rb', line 42 def initialize(name, type:, default:, owner:, policy:, allowed_values: nil, cli_option: nil) validate_identity!(name, owner, policy) validate_shape!(type, policy, cli_option) @name = name.dup.freeze @type = type @owner = owner @policy = policy @allowed_values = allowed_values&.map { |value| immutable(value) }&.freeze @cli_option = cli_option @default = validate(default) freeze end |
Instance Attribute Details
#allowed_values ⇒ Array[config_value]? (readonly)
37 38 39 |
# File 'lib/ibex/configuration.rb', line 37 def allowed_values @allowed_values end |
#cli_option ⇒ Symbol? (readonly)
38 39 40 |
# File 'lib/ibex/configuration.rb', line 38 def cli_option @cli_option end |
#default ⇒ config_value (readonly)
34 35 36 |
# File 'lib/ibex/configuration.rb', line 34 def default @default end |
#name ⇒ String (readonly)
32 33 34 |
# File 'lib/ibex/configuration.rb', line 32 def name @name end |
#owner ⇒ Symbol (readonly)
35 36 37 |
# File 'lib/ibex/configuration.rb', line 35 def owner @owner end |
#policy ⇒ Symbol (readonly)
36 37 38 |
# File 'lib/ibex/configuration.rb', line 36 def policy @policy end |
#type ⇒ Symbol (readonly)
33 34 35 |
# File 'lib/ibex/configuration.rb', line 33 def type @type end |
Instance Method Details
#immutable(value) ⇒ config_value
96 97 98 |
# File 'lib/ibex/configuration.rb', line 96 def immutable(value) value.is_a?(String) ? value.dup.freeze : value end |
#owner_name ⇒ String
70 71 72 |
# File 'lib/ibex/configuration.rb', line 70 def owner_name OWNER_NAMES.fetch(@owner) end |
#valid_type?(value) ⇒ Boolean
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ibex/configuration.rb', line 101 def valid_type?(value) case @type when :symbol then value.is_a?(Symbol) when :boolean then BOOLEAN_VALUES.include?(value) when :integer then value.is_a?(Integer) when :optional_boolean then value.nil? || BOOLEAN_VALUES.include?(value) when :optional_string then value.nil? || value.is_a?(String) else raise ArgumentError, "unknown configuration value type: #{@type.inspect}" end end |
#validate(value) ⇒ config_value
Validate and defensively freeze a value from an adapter or declaration.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ibex/configuration.rb', line 58 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 |
#validate_identity!(name, owner, policy) ⇒ void
This method returns an undefined value.
77 78 79 80 81 82 83 84 85 |
# File 'lib/ibex/configuration.rb', line 77 def validate_identity!(name, owner, policy) valid_name = name.is_a?(String) && name.match?(/\A[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+\z/) raise ArgumentError, "invalid canonical configuration key: #{name.inspect}" unless valid_name raise ArgumentError, "unknown configuration owner: #{owner.inspect}" unless OWNER_NAMES.key?(owner) raise ArgumentError, "unknown configuration policy: #{policy.inspect}" unless POLICIES.include?(policy) return if OWNER_POLICIES.fetch(owner) == policy raise ArgumentError, "#{owner} configuration must use #{OWNER_POLICIES.fetch(owner)} policy" end |
#validate_shape!(type, policy, cli_option) ⇒ void
This method returns an undefined value.
88 89 90 91 92 93 |
# File 'lib/ibex/configuration.rb', line 88 def validate_shape!(type, policy, cli_option) raise ArgumentError, "cli_option must be a Symbol or nil" unless cli_option.nil? || cli_option.is_a?(Symbol) return unless policy == :minimum && type != :integer raise ArgumentError, "minimum configuration keys must have Integer values" end |