Class: Wpxf::IntegerOption
- Defined in:
- lib/wpxf/core/opts/integer_option.rb
Overview
An integer option.
Instance Attribute Summary collapse
-
#max ⇒ Integer?
The highest valid value.
-
#min ⇒ Integer?
The lowest valid value.
Attributes inherited from Option
#advanced, #default, #desc, #enums, #evasion, #name, #regex, #required
Instance Method Summary collapse
-
#initialize(attrs) ⇒ IntegerOption
constructor
Initializes a named option.
-
#meets_max_requirement?(value) ⇒ Boolean
True if the value meets the maximum valid value requirement.
-
#meets_min_requirement?(value) ⇒ Boolean
True if the value meets the minimum valid value requirement.
-
#normalize(value) ⇒ Integer
A normalized value to conform with the type that the option is conveying.
-
#valid?(value) ⇒ Boolean
Check if the specified value is valid in the context of this option.
-
#valid_integer?(value) ⇒ Boolean
True if the value is a valid integer.
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.
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
#max ⇒ Integer?
Returns the highest valid value.
72 73 74 |
# File 'lib/wpxf/core/opts/integer_option.rb', line 72 def max @max end |
#min ⇒ Integer?
Returns 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.
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.
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.
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.
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.
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 |