Class: CommandMapper::Types::Num
- Defined in:
- lib/command_mapper/types/num.rb
Overview
Represents a numeric value.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#range ⇒ Range?
readonly
The optional range of acceptable numbers.
Instance Method Summary collapse
-
#format(value) ⇒ String
Formats a numeric value.
-
#initialize(range: nil) ⇒ Num
constructor
Initializes the numeric value.
-
#validate(value) ⇒ true, (false, String)
Validates a value.
Constructor Details
#initialize(range: nil) ⇒ Num
Initializes the numeric value.
23 24 25 |
# File 'lib/command_mapper/types/num.rb', line 23 def initialize(range: nil) @range = range end |
Instance Attribute Details
#range ⇒ Range? (readonly)
The optional range of acceptable numbers.
15 16 17 |
# File 'lib/command_mapper/types/num.rb', line 15 def range @range end |
Instance Method Details
#format(value) ⇒ String
Formats a numeric value.
73 74 75 76 77 78 |
# File 'lib/command_mapper/types/num.rb', line 73 def format(value) case value when Integer, String then value.to_s else value.to_i.to_s end end |
#validate(value) ⇒ true, (false, String)
Validates a value.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/command_mapper/types/num.rb', line 39 def validate(value) case value when Integer # no-op when String unless value =~ /\A\d+\z/ return [false, "contains non-numeric characters (#{value.inspect})"] end else unless value.respond_to?(:to_i) return [false, "cannot be converted into an Integer (#{value.inspect})"] end end if @range unless @range.include?(value.to_i) return [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"] end end return true end |