class Wpxf::Option

The base class for all module options.

Attributes

advanced[RW]

@return [Boolean] whether or not this is an advanced option.

default[RW]

@return [Object, nil] the default value of the option.

desc[RW]

@return [String] the description of the option.

enums[RW]

@return [Array, nil] the list of potential valid values.

evasion[RW]

@return [Boolean] whether or not this is an evasion option.

name[RW]

@return [String] the name of the option.

regex[RW]

@return [String, nil] an optional regex to validate the option value.

required[RW]

@return [Boolean] whether or not the option is required.

Public Class Methods

new(attrs) click to toggle source

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

advanced?() click to toggle source

@return [Boolean] true if this is an advanced option.

# File lib/wpxf/core/opts/option.rb, line 39
def advanced?
  advanced
end
display_value(value) click to toggle source

@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
empty?(value) click to toggle source

@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
empty_required_value?(value) click to toggle source

@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
evasion?() click to toggle source

@return [Boolean] true if this is an evasion option.

# File lib/wpxf/core/opts/option.rb, line 44
def evasion?
  evasion
end
normalize(value) click to toggle source

@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
required?() click to toggle source

@return [Boolean] true if this is a required option.

# File lib/wpxf/core/opts/option.rb, line 49
def required?
  required
end
update_optional_attributes(attrs) click to toggle source

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
valid?(value) click to toggle source

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
value?(value) click to toggle source

@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