class ForemanMaintain::Param

Attributes

description[R]
name[R]
options[R]

Public Class Methods

new(name, description, options, &block) click to toggle source
# File lib/foreman_maintain/param.rb, line 5
def initialize(name, description, options, &block)
  options.validate_options!(:description, :required, :flag, :array,
    :allowed_values, :default)
  @name = name
  @description = description || options[:description] || ''
  @options = options
  @required = @options.fetch(:required, false)
  @flag = @options.fetch(:flag, false)
  @block = block
  @allowed_values = @options.fetch(:allowed_values, [])
  @array = @options.fetch(:array, false)
  @default = @options.fetch(:default, nil)
end

Public Instance Methods

array?() click to toggle source
# File lib/foreman_maintain/param.rb, line 27
def array?
  @array
end
flag?() click to toggle source
# File lib/foreman_maintain/param.rb, line 19
def flag?
  @flag
end
process(value) click to toggle source
# File lib/foreman_maintain/param.rb, line 31
def process(value)
  # default values imply we can not pass nil if there is non-nil default
  value = @default if value.nil?
  value = process_array(value) if array?
  value = @block.call(value) if @block
  if value.nil? && required?
    raise ArgumentError, "Param #{name} is required but no value given"
  elsif flag?
    value = value ? true : false
  end
  validate_with_allowed_values(value)
  value
end
process_array(value) click to toggle source
# File lib/foreman_maintain/param.rb, line 45
def process_array(value)
  if value.is_a?(Array)
    value
  else
    value.to_s.split(',').map(&:strip)
  end
end
required?() click to toggle source
# File lib/foreman_maintain/param.rb, line 23
def required?
  @required
end
validate_with_allowed_values(value) click to toggle source
# File lib/foreman_maintain/param.rb, line 53
def validate_with_allowed_values(value)
  return if @allowed_values.empty?
  within_allowed = case value
                   when Array
                     (value - @allowed_values).empty?
                   when Symbol, String
                     @allowed_values.include?(value.to_s)
                   else
                     raise NotImplementedError
                   end
  return if within_allowed
  error_msg = "'#{value}' not allowed for #{name} param."
  raise ForemanMaintain::Error::UsageError,
    "#{error_msg} Possible values are #{@allowed_values.join(', ')}"
end