Class: Ibex::Configuration::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/configuration.rb

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:, 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_valuesObject (readonly)

: Array?



40
41
42
# File 'lib/ibex/configuration.rb', line 40

def allowed_values
  @allowed_values
end

#cli_aliasesObject (readonly)

: Array



43
44
45
# File 'lib/ibex/configuration.rb', line 43

def cli_aliases
  @cli_aliases
end

#cli_optionObject (readonly)

: Symbol?



41
42
43
# File 'lib/ibex/configuration.rb', line 41

def cli_option
  @cli_option
end

#defaultObject (readonly)

: config_value



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

def default
  @default
end

#nameObject (readonly)

: String



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

def name
  @name
end

#ownerObject (readonly)

: Symbol



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

def owner
  @owner
end

#parser_settingObject (readonly)

: Symbol?



42
43
44
# File 'lib/ibex/configuration.rb', line 42

def parser_setting
  @parser_setting
end

#typeObject (readonly)

: Symbol



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

def type
  @type
end

Instance Method Details

#owner_nameObject



78
79
80
# File 'lib/ibex/configuration.rb', line 78

def owner_name
  OWNER_NAMES.fetch(@owner)
end

#policyObject



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.

Raises:

  • (ArgumentError)


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