Class: ForemanMaintain::Param

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_maintain/param.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, options, &block) ⇒ Param

Returns a new instance of Param.



5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



3
4
5
# File 'lib/foreman_maintain/param.rb', line 3

def description
  @description
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/foreman_maintain/param.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/foreman_maintain/param.rb', line 3

def options
  @options
end

Instance Method Details

#array?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/foreman_maintain/param.rb', line 27

def array?
  @array
end

#flag?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/foreman_maintain/param.rb', line 19

def flag?
  @flag
end

#process(value) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# 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) ⇒ Object



45
46
47
48
49
50
51
# 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?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/foreman_maintain/param.rb', line 23

def required?
  @required
end

#validate_with_allowed_values(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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