Class: CommandMapper::Types::Dec
- Defined in:
- lib/command_mapper/types/dec.rb
Overview
Represents a decimal value (ex: 1.5
).
Instance Attribute Summary collapse
-
#range ⇒ Range<Float,Float>?
readonly
The optional range of acceptable decimal numbers.
Instance Method Summary collapse
-
#format(value) ⇒ String
Formats a decimal value.
-
#initialize(range: nil) ⇒ Dec
constructor
Initializes the decimal type.
-
#validate(value) ⇒ true, (false, String)
Validates a value.
Constructor Details
#initialize(range: nil) ⇒ Dec
Initializes the decimal type.
25 26 27 |
# File 'lib/command_mapper/types/dec.rb', line 25 def initialize(range: nil) @range = range end |
Instance Attribute Details
#range ⇒ Range<Float,Float>? (readonly)
The optional range of acceptable decimal numbers.
17 18 19 |
# File 'lib/command_mapper/types/dec.rb', line 17 def range @range end |
Instance Method Details
#format(value) ⇒ String
Formats a decimal value.
75 76 77 78 79 80 |
# File 'lib/command_mapper/types/dec.rb', line 75 def format(value) case value when Float, String then value.to_s else value.to_f.to_s end end |
#validate(value) ⇒ true, (false, String)
Validates a value.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/command_mapper/types/dec.rb', line 41 def validate(value) case value when Float # no-op when String unless value =~ /\A\d+(?:\.\d+)?\z/ return [false, "contains non-decimal characters (#{value.inspect})"] end else unless value.respond_to?(:to_f) return [false, "cannot be converted into a Float (#{value.inspect})"] end end if @range unless @range.include?(value.to_f) return [false, "(#{value.inspect}) not within the range of acceptable values (#{range.inspect})"] end end return true end |