Class: Unfig::ParamConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/unfig/param_config.rb

Overview

ParamConfig wraps the configuration of an individual param value, and all of the ways it can be supplied (a long flag, a short flag, a config-file entry, or an environment variable).

It enforces the allowed values on each piece of configuration, constructs defaults for several of them (based on the name), and exposes which of those methods are enabled for that variable (you can decide that --verbose is not suppliable through the config file for example). Each parameter can also be designated as multi?, meaning that it can be supplied multiple times (or supplied as an array, in the config-file case), and produces an array of values in any case.

Constant Summary collapse

KNOWN_ENABLED_VALUES =
["long", "short", "env", "file"].to_set.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data) ⇒ ParamConfig

Returns a new instance of ParamConfig.



18
19
20
21
22
# File 'lib/unfig/param_config.rb', line 18

def initialize(name, data)
  @name = name
  @data = data.transform_keys(&:to_sym)
  ParamValidator.new(@name, @data).validate!
end

Class Method Details

.load(params_data) ⇒ Object



16
# File 'lib/unfig/param_config.rb', line 16

def self.load(params_data) = params_data.map { |key, value| new(key, value) }

Instance Method Details

#defaultObject



30
# File 'lib/unfig/param_config.rb', line 30

def default = data.fetch(:default)

#descriptionObject



26
# File 'lib/unfig/param_config.rb', line 26

def description = data.fetch(:description)

#enabledObject



34
35
36
37
38
39
40
# File 'lib/unfig/param_config.rb', line 34

def enabled
  if data.key?(:enabled)
    data.fetch(:enabled)
  else
    KNOWN_ENABLED_VALUES.to_a.sort
  end
end

#envObject



58
59
60
61
62
63
64
# File 'lib/unfig/param_config.rb', line 58

def env
  if data.key?(:env)
    data.fetch(:env, nil)
  else
    name.upcase
  end
end

#longObject



42
43
44
45
46
47
48
# File 'lib/unfig/param_config.rb', line 42

def long
  if data.key?(:long)
    data.fetch(:long)
  else
    name.tr("_", "-").downcase
  end
end

#multi?Boolean

Returns:

  • (Boolean)


32
# File 'lib/unfig/param_config.rb', line 32

def multi? = data.fetch(:multi, false)

#nameObject



24
# File 'lib/unfig/param_config.rb', line 24

def name = @name.to_s

#shortObject



50
51
52
53
54
55
56
# File 'lib/unfig/param_config.rb', line 50

def short
  if data.key?(:short)
    data.fetch(:short)
  else
    name[0]
  end
end

#typeObject



28
# File 'lib/unfig/param_config.rb', line 28

def type = data.fetch(:type)