Class: PDK::Config::Value
- Inherits:
-
Object
- Object
- PDK::Config::Value
- 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
-
#default ⇒ Object?
Evaluate the default value block.
-
#default? ⇒ Boolean
True if the value has a default value block.
-
#default_to(&block) ⇒ nil
Assign a default value.
-
#initialize(name) ⇒ Value
constructor
Initialises an empty value definition.
-
#validate(validator) ⇒ nil
Assign a validator to the value.
-
#validate!(key, value) ⇒ nil
Validate a value against the assigned validators.
Constructor Details
#initialize(name) ⇒ Value
Initialises an empty value definition.
20 21 22 23 |
# File 'lib/pdk/config/value.rb', line 20 def initialize(name) @name = name @validators = [] end |
Instance Method Details
#default ⇒ Object?
Evaluate the default value block.
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.
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.
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.
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.
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 |