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 4
def initialize(name, description, options, &block)
  options.validate_options!(:description, :required, :flag, :array)
  @name = name
  @description = description || options[:description] || ''
  @options = options
  @required = @options.fetch(:required, false)
  @flag = @options.fetch(:flag, false)
  @block = block
  @array = @options.fetch(:array, false)
end

Public Instance Methods

array?() click to toggle source
# File lib/foreman_maintain/param.rb, line 23
def array?
  @array
end
flag?() click to toggle source
# File lib/foreman_maintain/param.rb, line 15
def flag?
  @flag
end
process(value) click to toggle source

rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity

# File lib/foreman_maintain/param.rb, line 28
def process(value)
  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
  value
end
process_array(value) click to toggle source
# File lib/foreman_maintain/param.rb, line 39
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 19
def required?
  @required
end