Class: Wpxf::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/wpxf/core/opts/option.rb

Overview

The base class for all module options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs) ⇒ Option

Initializes a named option.

Parameters:

  • 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



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

#advancedBoolean

Returns whether or not this is an advanced option.

Returns:

  • (Boolean)

    whether or not this is an advanced option.



110
111
112
# File 'lib/wpxf/core/opts/option.rb', line 110

def advanced
  @advanced
end

#defaultObject?

Returns the default value of the option.

Returns:

  • (Object, nil)

    the default value of the option.



107
108
109
# File 'lib/wpxf/core/opts/option.rb', line 107

def default
  @default
end

#descString

Returns the description of the option.

Returns:

  • (String)

    the description of the option.



104
105
106
# File 'lib/wpxf/core/opts/option.rb', line 104

def desc
  @desc
end

#enumsArray?

Returns the list of potential valid values.

Returns:

  • (Array, nil)

    the list of potential valid values.



116
117
118
# File 'lib/wpxf/core/opts/option.rb', line 116

def enums
  @enums
end

#evasionBoolean

Returns whether or not this is an evasion option.

Returns:

  • (Boolean)

    whether or not this is an evasion option.



113
114
115
# File 'lib/wpxf/core/opts/option.rb', line 113

def evasion
  @evasion
end

#nameString

Returns the name of the option.

Returns:

  • (String)

    the name of the option.



98
99
100
# File 'lib/wpxf/core/opts/option.rb', line 98

def name
  @name
end

#regexString?

Returns an optional regex to validate the option value.

Returns:

  • (String, nil)

    an optional regex to validate the option value.



119
120
121
# File 'lib/wpxf/core/opts/option.rb', line 119

def regex
  @regex
end

#requiredBoolean

Returns whether or not the option is required.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Parameters:

  • value

    the value to get the display value of.

Returns:

  • (String)

    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.

Parameters:

  • value

    the value to check.

Returns:

  • (Boolean)

    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.

Parameters:

  • value

    the value to check against.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Parameters:

  • value

    the value to normalize.

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.

Returns:

  • (Boolean)

    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.

Parameters:

  • attrs (Hash)

    the new values.



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.

Parameters:

  • value

    the value to validate.

Returns:

  • (Boolean)

    true if valid.



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.

Parameters:

  • value

    the value to check.

Returns:

  • (Boolean)

    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