Class: Wpxf::IntegerOption

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

Overview

An integer option.

Instance Attribute Summary collapse

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?, #required?, #update_optional_attributes, #value?

Constructor Details

#initialize(attrs) ⇒ IntegerOption

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

    • min: the lowest valid value

    • max: the highest valid value



19
20
21
22
23
24
# File 'lib/wpxf/core/opts/integer_option.rb', line 19

def initialize(attrs)
  super

  self.min = attrs[:min].to_i unless attrs[:min].nil?
  self.max = attrs[:max].to_i unless attrs[:max].nil?
end

Instance Attribute Details

#maxInteger?

Returns the highest valid value.

Returns:

  • (Integer, nil)

    the highest valid value.



72
73
74
# File 'lib/wpxf/core/opts/integer_option.rb', line 72

def max
  @max
end

#minInteger?

Returns the lowest valid value.

Returns:

  • (Integer, nil)

    the lowest valid value.



69
70
71
# File 'lib/wpxf/core/opts/integer_option.rb', line 69

def min
  @min
end

Instance Method Details

#meets_max_requirement?(value) ⇒ Boolean

Returns true if the value meets the maximum valid value requirement.

Parameters:

  • value

    the value to validate.

Returns:

  • (Boolean)

    true if the value meets the maximum valid value requirement.



53
54
55
# File 'lib/wpxf/core/opts/integer_option.rb', line 53

def meets_max_requirement?(value)
  max.nil? || (normalize(value) <= max)
end

#meets_min_requirement?(value) ⇒ Boolean

Returns true if the value meets the minimum valid value requirement.

Parameters:

  • value

    the value to validate.

Returns:

  • (Boolean)

    true if the value meets the minimum valid value requirement.



46
47
48
# File 'lib/wpxf/core/opts/integer_option.rb', line 46

def meets_min_requirement?(value)
  min.nil? || (normalize(value) >= min)
end

#normalize(value) ⇒ Integer

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

Parameters:

  • value

    the value to normalize.

Returns:

  • (Integer)

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



29
30
31
32
33
34
35
# File 'lib/wpxf/core/opts/integer_option.rb', line 29

def normalize(value)
  if value.to_s.match?(/^0x[a-fA-F\d]+$/)
    value.to_i(16)
  else
    value.to_i
  end
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.



60
61
62
63
64
65
66
# File 'lib/wpxf/core/opts/integer_option.rb', line 60

def valid?(value)
  return true if value.nil? && !required?
  return false unless valid_integer?(value)
  return false unless meets_min_requirement?(value)
  return false unless meets_max_requirement?(value)
  super
end

#valid_integer?(value) ⇒ Boolean

Returns true if the value is a valid integer.

Parameters:

  • value

    the value to validate.

Returns:

  • (Boolean)

    true if the value is a valid integer.



39
40
41
# File 'lib/wpxf/core/opts/integer_option.rb', line 39

def valid_integer?(value)
  value && !value.to_s.match(/^0x[0-9a-fA-F]+$|^-?\d+$/).nil?
end