Class: PDK::Config::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/config/value.rb

Overview

A class for describing the value of a PDK::Config setting.

Generally, this is never instantiated manually, but is instead instantiated by passing a block to Namespace#value.

PDK::Config::Namespace.new(‘analytics’) do

value :disabled do
  validate PDK::Config::Validator.boolean
  default_to { false }
end

end

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Value

Initialises an empty value definition.

Parameters:

  • name (String, Symbol)

    the name of the value.



20
21
22
23
# File 'lib/pdk/config/value.rb', line 20

def initialize(name)
  @name = name
  @validators = []
end

Instance Method Details

#defaultObject?

Evaluate the default value block.

Returns:

  • (Object, nil)

    the result of evaluating the block given to #default_to, or ‘nil` if the value has no default.



84
85
86
# File 'lib/pdk/config/value.rb', line 84

def default
  default? ? @default_to.call : nil
end

#default?Boolean

Returns true if the value has a default value block.

Returns:

  • (Boolean)

    true if the value has a default value block.



89
90
91
# File 'lib/pdk/config/value.rb', line 89

def default?
  !@default_to.nil?
end

#default_to(&block) ⇒ nil

Assign a default value.

Parameters:

  • block (Proc)

    a block that is lazy evaluated when necessary in order to determine the default value.

Returns:

  • (nil)

Raises:

  • (ArgumentError)


75
76
77
78
# File 'lib/pdk/config/value.rb', line 75

def default_to(&block)
  raise ArgumentError, _('must be passed a block') unless block_given?
  @default_to = block
end

#validate(validator) ⇒ nil

Assign a validator to the value.

Parameters:

  • validator (Hash{Symbol => [Proc,String]})

Options Hash (validator):

  • :proc (Proc)

    a lambda that takes the value to be validated as the argument and returns ‘true` if the value is valid.

  • :message (String)

    a description of what the validator is testing for, that is displayed to the user as part of the error message for invalid values.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if not passed a Hash.

  • (ArgumentError)

    if the Hash doesn’t have a ‘:proc` key that contains a Proc.

  • (ArgumentError)

    if the Hash doesn’t have a ‘:message` key that contains a String.



41
42
43
44
45
46
47
# File 'lib/pdk/config/value.rb', line 41

def validate(validator)
  raise ArgumentError, _('`validator` must be a Hash') unless validator.is_a?(Hash)
  raise ArgumentError, _('the :proc key must contain a Proc') unless validator.key?(:proc) && validator[:proc].is_a?(Proc)
  raise ArgumentError, _('the :message key must contain a String') unless validator.key?(:message) && validator[:message].is_a?(String)

  @validators << validator
end

#validate!(key, value) ⇒ nil

Validate a value against the assigned validators.

Parameters:

  • key (String)

    the name of the value being validated.

  • value (Object)

    the value being validated.

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if any of the assigned validators fail to validate the value.



58
59
60
61
62
63
64
65
66
67
# File 'lib/pdk/config/value.rb', line 58

def validate!(key, value)
  @validators.each do |validator|
    next if validator[:proc].call(value)

    raise ArgumentError, _('%{key} %{message}') % {
      key:     key,
      message: validator[:message],
    }
  end
end