Class: Msf::OptArray
Overview
Array option - allows multiple discrete values separated by a delimiter.
Constant Summary collapse
- DEFAULT_SEPARATOR =
Default separator - matches comma separated values
','
Instance Attribute Summary collapse
-
#accepted ⇒ Object
readonly
Accessor for accepted values.
-
#desc_string ⇒ Object
protected
:nodoc:.
Attributes inherited from OptBase
#advanced, #aliases, #conditions, #default, #enums, #evasion, #fallbacks, #max_length, #name, #owner, #regex, #required
Instance Method Summary collapse
-
#case_sensitive? ⇒ Boolean
protected
Determines if accepted values are case-sensitive Uses the same logic as OptEnum - if all accepted values are unique when downcased, then we're case-insensitive.
- #desc ⇒ Object
-
#desc=(value) ⇒ Object
Override desc to include accepted values if defined.
-
#display_value(value) ⇒ String
Returns a user-friendly display of the value.
-
#initialize(in_name, attrs = [], accepted: nil, separator: nil, strip_whitespace: true, unique: true, **kwargs) ⇒ OptArray
constructor
A new instance of OptArray.
-
#normalize(value = self.value) ⇒ Array?
Normalizes the value to an array with proper formatting.
- #type ⇒ Object
-
#valid?(value = self.value, check_empty: true, datastore: nil) ⇒ Boolean
Validates the array option value.
-
#value_to_array(value) ⇒ Array
protected
Converts a value to an array.
Methods inherited from OptBase
#advanced?, #empty_required_value?, #evasion?, #invalid_value_length?, #required?, #type?, #validate_on_assignment?
Constructor Details
#initialize(in_name, attrs = [], accepted: nil, separator: nil, strip_whitespace: true, unique: true, **kwargs) ⇒ OptArray
Returns a new instance of OptArray.
24 25 26 27 28 29 30 31 32 |
# File 'lib/msf/core/opt_array.rb', line 24 def initialize(in_name, attrs = [], accepted: nil, separator: nil, strip_whitespace: true, unique: true, **kwargs) super(in_name, attrs, **kwargs) @accepted = accepted ? [*accepted].map(&:to_s) : nil @separator = separator || DEFAULT_SEPARATOR @strip_whitespace = strip_whitespace @unique = unique end |
Instance Attribute Details
#accepted ⇒ Object (readonly)
Accessor for accepted values
115 116 117 |
# File 'lib/msf/core/opt_array.rb', line 115 def accepted @accepted end |
#desc_string ⇒ Object (protected)
:nodoc:
147 148 149 |
# File 'lib/msf/core/opt_array.rb', line 147 def desc_string @desc_string end |
Instance Method Details
#case_sensitive? ⇒ Boolean (protected)
Determines if accepted values are case-sensitive Uses the same logic as OptEnum - if all accepted values are unique when downcased, then we're case-insensitive
142 143 144 145 |
# File 'lib/msf/core/opt_array.rb', line 142 def case_sensitive? return true unless @accepted @accepted.map(&:downcase).uniq.length != @accepted.uniq.length end |
#desc ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/msf/core/opt_array.rb', line 104 def desc str = @desc_string || '' if @accepted accepted_str = @accepted.join(', ') "#{str} (Accepted: #{accepted_str})" else str end end |
#desc=(value) ⇒ Object
Override desc to include accepted values if defined
99 100 101 102 |
# File 'lib/msf/core/opt_array.rb', line 99 def desc=(value) @desc_string = value desc end |
#display_value(value) ⇒ String
Returns a user-friendly display of the value
91 92 93 94 95 96 |
# File 'lib/msf/core/opt_array.rb', line 91 def display_value(value) arr = value.is_a?(Array) ? value : value_to_array(value) arr.join(', ') rescue value.to_s end |
#normalize(value = self.value) ⇒ Array?
Normalizes the value to an array with proper formatting
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/msf/core/opt_array.rb', line 64 def normalize(value = self.value) return nil if value.nil? arr = value_to_array(value) # Normalize case if accepted values are defined and case-insensitive. # This must run before de-duplication so that case-variant spellings of # the same accepted value (e.g. "STDAPI" and "stdapi") collapse to one # canonical member rather than surviving #uniq as distinct strings. if @accepted && !case_sensitive? arr = arr.map do |member| @accepted.find { |a| a.casecmp?(member) } || member end end # Apply uniqueness if requested arr = arr.uniq if @unique # Return nil if validation fails return nil unless valid?(arr, check_empty: false) arr end |
#type ⇒ Object
13 14 15 |
# File 'lib/msf/core/opt_array.rb', line 13 def type 'array' end |
#valid?(value = self.value, check_empty: true, datastore: nil) ⇒ Boolean
Validates the array option value
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/msf/core/opt_array.rb', line 39 def valid?(value = self.value, check_empty: true, datastore: nil) return false if check_empty && required? && value.to_s.empty? return true if value.nil? && !required? return false if value.nil? # Normalize to array arr = value_to_array(value) # If accepted values are defined, validate each member if @accepted arr.all? do |member| if case_sensitive? @accepted.include?(member) else @accepted.map(&:downcase).include?(member.downcase) end end else true end end |
#value_to_array(value) ⇒ Array (protected)
Converts a value to an array
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/msf/core/opt_array.rb', line 122 def value_to_array(value) return value if value.is_a?(Array) return [] if value.nil? || value.to_s.empty? # Split by separator arr = value.to_s.split(@separator) # Strip whitespace and remove empty strings from each member if requested if @strip_whitespace arr = arr.map(&:strip) arr.reject!(&:empty?) end arr end |