module Wpxf::Options

A mixin to provide option registering and datastore functionality.

Attributes

datastore[RW]

@return [Hash] a hash containing the option values specified by the user.

options[RW]

@return [Array] an array of {Option} objects used to configure

the current module.

Public Class Methods

new() click to toggle source

Initialize a new instance of {Options}.

Calls superclass method
# File lib/wpxf/core/options.rb, line 15
def initialize
  super

  self.options = []
  self.datastore = {}
end

Public Instance Methods

all_options_valid?() click to toggle source

@return [Boolean] true if all the required options are set.

# File lib/wpxf/core/options.rb, line 142
def all_options_valid?
  options.each do |opt|
    return false unless opt.valid?(datastore[opt.name])
  end

  true
end
get_option(name) click to toggle source

Find and return an {Option} by its registered name. @param name the name of the {Option}. @return [Option, nil] the matching option or nil if not found.

# File lib/wpxf/core/options.rb, line 73
def get_option(name)
  options.find { |o| o.name.eql?(name) }
end
get_option_value(name) click to toggle source

Get the value of a module option. @param name the name of the option. @return the option value.

# File lib/wpxf/core/options.rb, line 94
def get_option_value(name)
  datastore[name]
end
missing_options() click to toggle source

@return [Array] an array of missing option names that are required.

# File lib/wpxf/core/options.rb, line 160
def missing_options
  opts = []
  options.each do |opt|
    opts.push(opt.name) unless !opt.required? || option_valid?(opt)
  end
  opts
end
normalized_option_value(name) click to toggle source

Get the normalized value of a module option. @param name the name of the option. @return the option value.

# File lib/wpxf/core/options.rb, line 101
def normalized_option_value(name)
  option = get_option(name)
  return option.normalize(datastore[name]) unless option.nil?
end
option_valid?(opt) click to toggle source

Check if an option is valid. @param opt [String, Option] the {Option} or name of the option to check. @return [Boolean] true if valid.

# File lib/wpxf/core/options.rb, line 153
def option_valid?(opt)
  return opt.valid?(datastore[opt.name]) if opt.is_a? Option

  get_option(opt).valid?(datastore[opt])
end
option_value?(name) click to toggle source

@param name the name of the option. @return [Boolean] true if the specified option has a value.

# File lib/wpxf/core/options.rb, line 108
def option_value?(name)
  !datastore[name].nil? && !datastore[name].empty?
end
register_advanced_options(opts) click to toggle source

Register an array of advanced {Option}. @param opts the array of {Option} to register. @return [Void] nothing.

# File lib/wpxf/core/options.rb, line 51
def register_advanced_options(opts)
  opts.each do |opt|
    opt.advanced = true
  end

  register_options(opts)
end
register_evasion_options(opts) click to toggle source

Register an array of evasion {Option}. @param opts the array of {Option} to register. @return [Void] nothing.

# File lib/wpxf/core/options.rb, line 62
def register_evasion_options(opts)
  opts.each do |opt|
    opt.evasion = true
  end

  register_options(opts)
end
register_option(opt) click to toggle source

Register an {Option}. @param opt the {Option} to register. @return [Void] nothing.

# File lib/wpxf/core/options.rb, line 32
def register_option(opt)
  raise 'payload is a reserved name' if opt.name.eql? 'payload'
  unregister_option(opt)
  options.push(opt)
  datastore[opt.name] = opt.default unless opt.default.nil?
end
register_options(opts) click to toggle source

Register an array of {Option}. @param opts the array of {Option} to register. @return [Void] nothing.

# File lib/wpxf/core/options.rb, line 42
def register_options(opts)
  opts.each do |opt|
    register_option(opt)
  end
end
scoped_option_change(name, value) { |get_option_value(name)| ... } click to toggle source

Temporarily change the value of an option and yield a block that uses the scoped value before resetting it back to the original value. @param name [String] the name of the option. @param value [Object] the scoped value. @yieldparam value [Object] the scoped value of the option. @return [Nil] nothing.

# File lib/wpxf/core/options.rb, line 118
def scoped_option_change(name, value)
  original_value = get_option_value(name)

  # Set the scoped option value and invoke the proc.
  set_option_value(name, value)
  yield(get_option_value(name))

  # Reset the option value back to the original.
  set_option_value(name, original_value)

  nil
end
set_option_value(name, value) click to toggle source

Set the value of a module option. @param name the name of the option to set. @param value the value to use. @return [String, Symbol] the normalized value, :invalid if the

specified value is invalid or :not_found if the name is invalid.
# File lib/wpxf/core/options.rb, line 82
def set_option_value(name, value)
  opt = get_option(name)
  return :not_found unless opt
  return :invalid unless opt.valid?(value)

  datastore[name] = value
  opt.normalize(value)
end
unregister_option(opt) click to toggle source

Unregister an {Option}. @param opt the {Option} to unregister. @return [Void] nothing.

# File lib/wpxf/core/options.rb, line 25
def unregister_option(opt)
  options.delete_if { |o| o.name.eql?(opt.name) }
end
unset_option(name) click to toggle source

Unset an option or reset it back to its default value. @param name [String] the name of the option to unset.

# File lib/wpxf/core/options.rb, line 133
def unset_option(name)
  opt = get_option(name)
  return unless opt

  datastore.delete(name)
  datastore[opt.name] = opt.default if opt.required?
end