class Wpxf::Option
The base class for all module options.
Attributes
@return [Boolean] whether or not this is an advanced option.
@return [Object, nil] the default value of the option.
@return [String] the description of the option.
@return [Array, nil] the list of potential valid values.
@return [Boolean] whether or not this is an evasion option.
@return [String] the name of the option.
@return [String, nil] an optional regex to validate the option value.
@return [Boolean] whether or not the option is required.
Public Class Methods
Initializes a named option. @param attrs an object containing the following values
* *name*: the name of the option (required) * *desc*: the description of the option (required) * *required*: whether or not the option is required * *default*: the default value of the option * *advanced*: whether or not this is an advanced option * *evasion*: whether or not this is an evasion option * *enums*: the list of potential valid values * *regex*: regex to validate the option value
# File lib/wpxf/core/opts/option.rb, line 17 def initialize(attrs) if attrs[:name].nil? && attrs[:desc].nil? raise 'A value must be specified for :name and :desc' end self.name = attrs[:name] self.desc = attrs[:desc] update_optional_attributes(attrs) end
Public Instance Methods
@return [Boolean] true if this is an advanced option.
# File lib/wpxf/core/opts/option.rb, line 39 def advanced? advanced end
@param value the value to get the display value of. @return [String] a string representing a user-friendly display of
the chosen value.
# File lib/wpxf/core/opts/option.rb, line 93 def display_value(value) value.to_s end
@param value the value to check. @return [Boolean] true if the value is nil or empty.
# File lib/wpxf/core/opts/option.rb, line 66 def empty?(value) value.nil? || value.to_s.empty? end
@param value the value to check against. @return [Boolean] true if the value supplied is nil or empty and
it's required to be a valid value.
# File lib/wpxf/core/opts/option.rb, line 79 def empty_required_value?(value) required? && empty?(value) end
@return [Boolean] true if this is an evasion option.
# File lib/wpxf/core/opts/option.rb, line 44 def evasion? evasion end
@param value the value to normalize. @return a normalized value to conform with the type that the option
is conveying.
# File lib/wpxf/core/opts/option.rb, line 86 def normalize(value) value end
@return [Boolean] true if this is a required option.
# File lib/wpxf/core/opts/option.rb, line 49 def required? required end
Update the optional attributes of the option. @param attrs [Hash] the new values.
# File lib/wpxf/core/opts/option.rb, line 29 def update_optional_attributes(attrs) self.required = attrs[:required] || false self.default = attrs[:default] self.advanced = attrs[:advanced] || false self.evasion = attrs[:evasion] || false self.enums = attrs[:enums] self.regex = attrs[:regex] end
Checks if the specified value is valid in the context of this option. @param value the value to validate. @return [Boolean] true if valid.
# File lib/wpxf/core/opts/option.rb, line 56 def valid?(value) return false if empty_required_value?(value) return true if !required? && empty?(value) return value.to_s.match?(regex) if regex true end
@param value the value to check. @return [Boolean] true if the value is not nil or empty.
# File lib/wpxf/core/opts/option.rb, line 72 def value?(value) !empty?(value) end