Class: Wpxf::Option
- Inherits:
-
Object
- Object
- Wpxf::Option
- Defined in:
- lib/wpxf/core/opts/option.rb
Overview
The base class for all module options.
Direct Known Subclasses
BooleanOption, EnumOption, IntegerOption, PathOption, PortOption, StringOption
Instance Attribute Summary collapse
-
#advanced ⇒ Boolean
Whether or not this is an advanced option.
-
#default ⇒ Object?
The default value of the option.
-
#desc ⇒ String
The description of the option.
-
#enums ⇒ Array?
The list of potential valid values.
-
#evasion ⇒ Boolean
Whether or not this is an evasion option.
-
#name ⇒ String
The name of the option.
-
#regex ⇒ String?
An optional regex to validate the option value.
-
#required ⇒ Boolean
Whether or not the option is required.
Instance Method Summary collapse
-
#advanced? ⇒ Boolean
True if this is an advanced option.
-
#display_value(value) ⇒ String
A string representing a user-friendly display of the chosen value.
-
#empty?(value) ⇒ Boolean
True if the value is nil or empty.
-
#empty_required_value?(value) ⇒ Boolean
True if the value supplied is nil or empty and it's required to be a valid value.
-
#evasion? ⇒ Boolean
True if this is an evasion option.
-
#initialize(attrs) ⇒ Option
constructor
Initializes a named option.
-
#normalize(value) ⇒ Object
A normalized value to conform with the type that the option is conveying.
-
#required? ⇒ Boolean
True if this is a required option.
-
#update_optional_attributes(attrs) ⇒ Object
Update the optional attributes of the option.
-
#valid?(value) ⇒ Boolean
Checks if the specified value is valid in the context of this option.
-
#value?(value) ⇒ Boolean
True if the value is not nil or empty.
Constructor Details
#initialize(attrs) ⇒ Option
Initializes a named option.
17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#advanced ⇒ Boolean
Returns whether or not this is an advanced option.
110 111 112 |
# File 'lib/wpxf/core/opts/option.rb', line 110 def advanced @advanced end |
#default ⇒ Object?
Returns the default value of the option.
107 108 109 |
# File 'lib/wpxf/core/opts/option.rb', line 107 def default @default end |
#desc ⇒ String
Returns the description of the option.
104 105 106 |
# File 'lib/wpxf/core/opts/option.rb', line 104 def desc @desc end |
#enums ⇒ Array?
Returns the list of potential valid values.
116 117 118 |
# File 'lib/wpxf/core/opts/option.rb', line 116 def enums @enums end |
#evasion ⇒ Boolean
Returns whether or not this is an evasion option.
113 114 115 |
# File 'lib/wpxf/core/opts/option.rb', line 113 def evasion @evasion end |
#name ⇒ String
Returns the name of the option.
98 99 100 |
# File 'lib/wpxf/core/opts/option.rb', line 98 def name @name end |
#regex ⇒ String?
Returns an optional regex to validate the option value.
119 120 121 |
# File 'lib/wpxf/core/opts/option.rb', line 119 def regex @regex end |
#required ⇒ Boolean
Returns whether or not the option is required.
101 102 103 |
# File 'lib/wpxf/core/opts/option.rb', line 101 def required @required end |
Instance Method Details
#advanced? ⇒ Boolean
Returns true if this is an advanced option.
39 40 41 |
# File 'lib/wpxf/core/opts/option.rb', line 39 def advanced? advanced end |
#display_value(value) ⇒ String
Returns a string representing a user-friendly display of the chosen value.
93 94 95 |
# File 'lib/wpxf/core/opts/option.rb', line 93 def display_value(value) value.to_s end |
#empty?(value) ⇒ Boolean
Returns true if the value is nil or empty.
66 67 68 |
# File 'lib/wpxf/core/opts/option.rb', line 66 def empty?(value) value.nil? || value.to_s.empty? end |
#empty_required_value?(value) ⇒ Boolean
Returns true if the value supplied is nil or empty and it's required to be a valid value.
79 80 81 |
# File 'lib/wpxf/core/opts/option.rb', line 79 def empty_required_value?(value) required? && empty?(value) end |
#evasion? ⇒ Boolean
Returns true if this is an evasion option.
44 45 46 |
# File 'lib/wpxf/core/opts/option.rb', line 44 def evasion? evasion end |
#normalize(value) ⇒ Object
Returns a normalized value to conform with the type that the option is conveying.
86 87 88 |
# File 'lib/wpxf/core/opts/option.rb', line 86 def normalize(value) value end |
#required? ⇒ Boolean
Returns true if this is a required option.
49 50 51 |
# File 'lib/wpxf/core/opts/option.rb', line 49 def required? required end |
#update_optional_attributes(attrs) ⇒ Object
Update the optional attributes of the option.
29 30 31 32 33 34 35 36 |
# 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) ⇒ Boolean
Checks if the specified value is valid in the context of this option.
56 57 58 59 60 61 62 |
# 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) ⇒ Boolean
Returns true if the value is not nil or empty.
72 73 74 |
# File 'lib/wpxf/core/opts/option.rb', line 72 def value?(value) !empty?(value) end |