Class: ForemanMaintain::Param
- Inherits:
-
Object
- Object
- ForemanMaintain::Param
- Defined in:
- lib/foreman_maintain/param.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #flag? ⇒ Boolean
-
#initialize(name, description, options, &block) ⇒ Param
constructor
A new instance of Param.
-
#process(value) ⇒ Object
rubocop:disable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity.
-
#process_array(value) ⇒ Object
rubocop:enable Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity.
- #required? ⇒ Boolean
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, , &block) .(:description, :required, :flag, :array) @name = name @description = description || [:description] || '' @options = @required = @options.fetch(:required, false) @flag = @options.fetch(:flag, false) @block = block @array = @options.fetch(:array, false) end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/foreman_maintain/param.rb', line 3 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/foreman_maintain/param.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/foreman_maintain/param.rb', line 3 def @options end |
Instance Method Details
#array? ⇒ Boolean
24 25 26 |
# File 'lib/foreman_maintain/param.rb', line 24 def array? @array end |
#flag? ⇒ 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
20 21 22 |
# File 'lib/foreman_maintain/param.rb', line 20 def required? @required end |