Class: Wpxf::BooleanOption

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

Overview

A boolean 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

#false?(value) ⇒ Boolean

Returns true if the value is a false boolean value.

Parameters:

  • value

    the value to check.

Returns:

  • (Boolean)

    true if the value is a false boolean value.



33
34
35
# File 'lib/wpxf/core/opts/boolean_option.rb', line 33

def false?(value)
  !true?(value)
end

#normalize(value) ⇒ Boolean

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

Parameters:

  • value

    the value to normalize.

Returns:

  • (Boolean)

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



21
22
23
# File 'lib/wpxf/core/opts/boolean_option.rb', line 21

def normalize(value)
  valid?(value) && !value.to_s.match(/^(y|yes|t|1|true)$/i).nil?
end

#true?(value) ⇒ Boolean

Returns true if the value is a true boolean value.

Parameters:

  • value

    the value to check.

Returns:

  • (Boolean)

    true if the value is a true boolean value.



27
28
29
# File 'lib/wpxf/core/opts/boolean_option.rb', line 27

def true?(value)
  normalize(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.



10
11
12
13
14
15
16
# File 'lib/wpxf/core/opts/boolean_option.rb', line 10

def valid?(value)
  return false if empty_required_value?(value)
  return true if !required? && empty?(value)

  pattern = /^(y|yes|n|no|t|f|0|1|true|false)$/i
  value?(value) && !value.to_s.match(pattern).nil?
end