Class: Wpxf::StringOption

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

Overview

A string option.

Instance Attribute Summary

Attributes inherited from Option

#advanced, #default, #desc, #enums, #evasion, #name, #regex, #required

Instance Method Summary collapse

Methods inherited from Option

#advanced?, #display_value, #empty?, #empty_required_value?, #evasion?, #initialize, #required?, #update_optional_attributes, #value?

Constructor Details

This class inherits a constructor from Wpxf::Option

Instance Method Details

#normalize(value) ⇒ String

Returns a normalized value to conform with the type that the option is conveying.

Parameters:

  • value

    the value to normalize. If a string starting with “file:” is specified, the path following it will be used to populate the value from the contents of the file.

Returns:

  • (String)

    a normalized value to conform with the type that the option is conveying.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wpxf/core/opts/string_option.rb', line 11

def normalize(value)
  match = value&.match(/^file:(.*)/)
  if match
    path = match[1]
    begin
      value = File.read(path)
    rescue ::Errno::ENOENT, ::Errno::EISDIR
      value = nil
    end
  end

  value
end

#valid?(value) ⇒ Boolean

Check if the specified value is valid in the context of this option.

Parameters:

  • value

    the value to validate.

Returns:

  • (Boolean)

    true if valid.



28
29
30
31
# File 'lib/wpxf/core/opts/string_option.rb', line 28

def valid?(value)
  value = normalize(value)
  super(value)
end