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.



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?



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

def max
  @max
end

#minInteger?



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



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



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



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



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