Module: Wpxf::Options
Overview
A mixin to provide option registering and datastore functionality.
Instance Attribute Summary collapse
-
#datastore ⇒ Hash
A hash containing the option values specified by the user.
-
#options ⇒ Array
An array of Option objects used to configure the current module.
Instance Method Summary collapse
-
#all_options_valid? ⇒ Boolean
True if all the required options are set.
-
#get_option(name) ⇒ Option?
Find and return an Option by its registered name.
-
#get_option_value(name) ⇒ Object
Get the value of a module option.
-
#initialize ⇒ Object
Initialize a new instance of Options.
-
#missing_options ⇒ Array
An array of missing option names that are required.
-
#normalized_option_value(name) ⇒ Object
Get the normalized value of a module option.
-
#option_valid?(opt) ⇒ Boolean
Check if an option is valid.
-
#option_value?(name) ⇒ Boolean
True if the specified option has a value.
-
#register_advanced_options(opts) ⇒ Void
Register an array of advanced Option.
-
#register_evasion_options(opts) ⇒ Void
Register an array of evasion Option.
-
#register_option(opt) ⇒ Void
Register an Option.
-
#register_options(opts) ⇒ Void
Register an array of Option.
-
#scoped_option_change(name, value) {|value| ... } ⇒ Nil
Temporarily change the value of an option and yield a block that uses the scoped value before resetting it back to the original value.
-
#set_option_value(name, value) ⇒ String, Symbol
Set the value of a module option.
-
#unregister_option(opt) ⇒ Void
Unregister an Option.
-
#unset_option(name) ⇒ Object
Unset an option or reset it back to its default value.
Instance Attribute Details
#datastore ⇒ Hash
Returns a hash containing the option values specified by the user.
173 174 175 |
# File 'lib/wpxf/core/options.rb', line 173 def datastore @datastore end |
#options ⇒ Array
Returns an array of Wpxf::Option objects used to configure the current module.
170 171 172 |
# File 'lib/wpxf/core/options.rb', line 170 def @options end |
Instance Method Details
#all_options_valid? ⇒ Boolean
Returns true if all the required options are set.
142 143 144 145 146 147 148 |
# File 'lib/wpxf/core/options.rb', line 142 def .each do |opt| return false unless opt.valid?(datastore[opt.name]) end true end |
#get_option(name) ⇒ Option?
Find and return an Wpxf::Option by its registered name.
73 74 75 |
# File 'lib/wpxf/core/options.rb', line 73 def get_option(name) .find { |o| o.name.eql?(name) } end |
#get_option_value(name) ⇒ Object
Get the value of a module option.
94 95 96 |
# File 'lib/wpxf/core/options.rb', line 94 def get_option_value(name) datastore[name] end |
#initialize ⇒ Object
Initialize a new instance of Wpxf::Options.
15 16 17 18 19 20 |
# File 'lib/wpxf/core/options.rb', line 15 def initialize super self. = [] self.datastore = {} end |
#missing_options ⇒ Array
Returns an array of missing option names that are required.
160 161 162 163 164 165 166 |
# File 'lib/wpxf/core/options.rb', line 160 def opts = [] .each do |opt| opts.push(opt.name) unless !opt.required? || option_valid?(opt) end opts end |
#normalized_option_value(name) ⇒ Object
Get the normalized value of a module option.
101 102 103 104 |
# 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) ⇒ Boolean
Check if an option is valid.
153 154 155 156 157 |
# 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) ⇒ Boolean
Returns true if the specified option has a value.
108 109 110 |
# File 'lib/wpxf/core/options.rb', line 108 def option_value?(name) !datastore[name].nil? && !datastore[name].empty? end |
#register_advanced_options(opts) ⇒ Void
Register an array of advanced Wpxf::Option.
51 52 53 54 55 56 57 |
# File 'lib/wpxf/core/options.rb', line 51 def (opts) opts.each do |opt| opt.advanced = true end (opts) end |
#register_evasion_options(opts) ⇒ Void
Register an array of evasion Wpxf::Option.
62 63 64 65 66 67 68 |
# File 'lib/wpxf/core/options.rb', line 62 def (opts) opts.each do |opt| opt.evasion = true end (opts) end |
#register_option(opt) ⇒ Void
Register an Wpxf::Option.
32 33 34 35 36 37 |
# 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) .push(opt) datastore[opt.name] = opt.default unless opt.default.nil? end |
#register_options(opts) ⇒ Void
Register an array of Wpxf::Option.
42 43 44 45 46 |
# File 'lib/wpxf/core/options.rb', line 42 def (opts) opts.each do |opt| register_option(opt) end end |
#scoped_option_change(name, value) {|value| ... } ⇒ Nil
Temporarily change the value of an option and yield a block that uses the scoped value before resetting it back to the original value.
118 119 120 121 122 123 124 125 126 127 128 129 |
# 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) ⇒ String, Symbol
Set the value of a module option.
82 83 84 85 86 87 88 89 |
# 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) ⇒ Void
Unregister an Wpxf::Option.
25 26 27 |
# File 'lib/wpxf/core/options.rb', line 25 def unregister_option(opt) .delete_if { |o| o.name.eql?(opt.name) } end |
#unset_option(name) ⇒ Object
Unset an option or reset it back to its default value.
133 134 135 136 137 138 139 |
# 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 |