Class: Ibex::Configuration::Key

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

Instance Method Summary collapse

Constructor Details

#initialize(name, type:, default:, owner:, policy:, allowed_values: nil, cli_option: nil) ⇒ Key

Returns a new instance of Key.

RBS:

  • (String name, type: Symbol, default: config_value, owner: Symbol, policy: Symbol, ?allowed_values: Array[config_value]?, ?cli_option: Symbol?) -> void

Parameters:

  • name (String)
  • type: (Symbol)
  • default: (config_value)
  • owner: (Symbol)
  • policy: (Symbol)
  • allowed_values: (Array[config_value], nil) (defaults to: nil)
  • cli_option: (Symbol, nil) (defaults to: nil)


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_valuesArray[config_value]? (readonly)

Signature:

  • Array[config_value]?

Returns:

  • (Array[config_value], nil)


37
38
39
# File 'lib/ibex/configuration.rb', line 37

def allowed_values
  @allowed_values
end

#cli_optionSymbol? (readonly)

Signature:

  • Symbol?

Returns:

  • (Symbol, nil)


38
39
40
# File 'lib/ibex/configuration.rb', line 38

def cli_option
  @cli_option
end

#defaultconfig_value (readonly)

Signature:

  • config_value

Returns:

  • (config_value)


34
35
36
# File 'lib/ibex/configuration.rb', line 34

def default
  @default
end

#nameString (readonly)

Signature:

  • String

Returns:

  • (String)


32
33
34
# File 'lib/ibex/configuration.rb', line 32

def name
  @name
end

#ownerSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


35
36
37
# File 'lib/ibex/configuration.rb', line 35

def owner
  @owner
end

#policySymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


36
37
38
# File 'lib/ibex/configuration.rb', line 36

def policy
  @policy
end

#typeSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


33
34
35
# File 'lib/ibex/configuration.rb', line 33

def type
  @type
end

Instance Method Details

#immutable(value) ⇒ config_value

RBS:

  • (config_value value) -> config_value

Parameters:

  • value (config_value)

Returns:

  • (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_nameString

RBS:

  • () -> String

Returns:

  • (String)


70
71
72
# File 'lib/ibex/configuration.rb', line 70

def owner_name
  OWNER_NAMES.fetch(@owner)
end

#valid_type?(value) ⇒ Boolean

RBS:

  • (config_value value) -> bool

Parameters:

  • value (config_value)

Returns:

  • (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.

RBS:

  • (config_value value) -> config_value

Parameters:

  • value (config_value)

Returns:

  • (config_value)


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.

RBS:

  • (String name, Symbol owner, Symbol policy) -> void

Parameters:

  • name (String)
  • owner (Symbol)
  • policy (Symbol)


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.

RBS:

  • (Symbol type, Symbol policy, Symbol? cli_option) -> void

Parameters:

  • type (Symbol)
  • policy (Symbol)
  • cli_option (Symbol, nil)


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