lib/bolt/config/transport/base.rb in bolt-2.15.0 vs lib/bolt/config/transport/base.rb in bolt-2.16.0
- old
+ new
@@ -1,14 +1,17 @@
# frozen_string_literal: true
require 'bolt/error'
require 'bolt/util'
+require 'bolt/config/transport/options'
module Bolt
class Config
module Transport
class Base
+ include Bolt::Config::Transport::Options
+
attr_reader :input
def initialize(data = {}, project = nil)
assert_hash_or_config(data)
@input = data
@@ -94,11 +97,11 @@
end
self.class::DEFAULTS
end
private def filter(unfiltered)
- unfiltered.slice(*self.class.options.keys)
+ unfiltered.slice(*self.class.options)
end
private def assert_hash_or_config(data)
return if data.is_a?(Hash) || data.is_a?(self.class)
raise Bolt::ValidationError,
@@ -114,27 +117,24 @@
# Validation defaults to just asserting the option types
private def validate
assert_type
end
- # Validates that each option is the correct type. Types are loaded from the OPTIONS hash.
+ # Validates that each option is the correct type. Types are loaded from the TRANSPORT_OPTIONS hash.
private def assert_type
@config.each_pair do |opt, val|
- next unless (type = self.class.options.dig(opt, :type))
+ types = Array(TRANSPORT_OPTIONS.dig(opt, :type)).compact
- # Options that accept a Boolean value are indicated by the type TrueClass, so we
- # need some special handling here to check if the value is either true or false.
- if type == TrueClass
- unless [true, false].include?(val)
- raise Bolt::ValidationError,
- "#{opt} must be a Boolean true or false, received #{val.class} #{val.inspect}"
- end
- else
- unless val.nil? || val.is_a?(type)
- raise Bolt::ValidationError,
- "#{opt} must be a #{type}, received #{val.class} #{val.inspect}"
- end
+ next if val.nil? || types.empty? || types.include?(val.class)
+
+ # Ruby doesn't have a Boolean class, so add it to the types list if TrueClass or FalseClass
+ # are present.
+ if types.include?(TrueClass) || types.include?(FalseClass)
+ types = types - [TrueClass, FalseClass] + ['Boolean']
end
+
+ raise Bolt::ValidationError,
+ "#{opt} must be of type #{types.join(', ')}; received #{val.class} #{val.inspect} "
end
end
end
end
end