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
# File 'lib/foreman_maintain/param.rb', line 5

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

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)


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

def array?
  @array
end

#flag?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/foreman_maintain/param.rb', line 16

def flag?
  @flag
end

#process(value) ⇒ Object

rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity



29
30
31
32
33
34
35
36
37
38
# File 'lib/foreman_maintain/param.rb', line 29

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) ⇒ Object

rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity



41
42
43
44
45
46
47
# File 'lib/foreman_maintain/param.rb', line 41

def process_array(value)
  if value.is_a?(Array)
    value
  else
    value.to_s.split(',').map(&:strip)
  end
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  @required
end