Module: BarkestCore::NumberParser
- Included in:
- MsSqlFunction
- Defined in:
- lib/barkest_core/concerns/number_parser.rb
Overview
This module will add number parsing methods to a class.
Constant Summary collapse
- NUMBER_REGEX =
This regular expression should match any non-exponential numeric value.
/^[\-\+]?(?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?$/
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
-
.parse_for_float_column(value) ⇒ Object
Parses a value for storage in a float column.
-
.parse_for_float_filter(value) ⇒ Object
Parses a value for use as a SQL filter.
-
.parse_for_int_column(value) ⇒ Object
Parses a value for storage in an integer column.
-
.parse_for_int_filter(value) ⇒ Object
Parses a value for use as a SQL filter.
Class Method Details
.included(base) ⇒ Object
:nodoc:
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/barkest_core/concerns/number_parser.rb', line 58 def self.included(base) base.class_eval do ## # Parses a value for storage in a float column. # # Returns nil if the value is invalid. # Otherwise it returns a float. # def self.parse_for_float_column(value) BarkestCore::NumberParser.parse_for_float_column value end ## # Parses a value for storage in an integer column. # # Returns nil if the value is invalid. # Otherwise it returns an integer. def self.parse_for_int_column(value) BarkestCore::NumberParser.parse_for_int_column value end ## # Parses a value for use as a SQL filter. # # Returns 'NULL' if the value parses to nil. # Otherwise returns the value. # def self.parse_for_float_filter(value) BarkestCore::NumberParser.parse_for_float_filter value end ## # Parses a value for use as a SQL filter. # # Returns 'NULL' if the value parses to nil. # Otherwise returns the value. # def self.parse_for_int_filter(value) BarkestCore::NumberParser.parse_for_int_filter value end end end |
.parse_for_float_column(value) ⇒ Object
Parses a value for storage in a float column.
Returns nil if the value is invalid. Otherwise it returns a float.
17 18 19 20 21 |
# File 'lib/barkest_core/concerns/number_parser.rb', line 17 def self.parse_for_float_column(value) value = value.to_s return nil unless NUMBER_REGEX.match(value) value.blank? ? nil : value.split(',').join.to_f end |
.parse_for_float_filter(value) ⇒ Object
Parses a value for use as a SQL filter.
Returns ‘NULL’ if the value parses to nil. Otherwise returns the value.
40 41 42 43 |
# File 'lib/barkest_core/concerns/number_parser.rb', line 40 def self.parse_for_float_filter(value) value = parse_for_float_column(value) value.nil? ? 'NULL' : value.to_s end |
.parse_for_int_column(value) ⇒ Object
Parses a value for storage in an integer column.
Returns nil if the value is invalid. Otherwise it returns an integer.
28 29 30 31 32 |
# File 'lib/barkest_core/concerns/number_parser.rb', line 28 def self.parse_for_int_column(value) value = value.to_s return nil unless NUMBER_REGEX.match(value) value.blank? ? nil : value.split(',').join.to_i end |
.parse_for_int_filter(value) ⇒ Object
Parses a value for use as a SQL filter.
Returns ‘NULL’ if the value parses to nil. Otherwise returns the value.
51 52 53 54 |
# File 'lib/barkest_core/concerns/number_parser.rb', line 51 def self.parse_for_int_filter(value) value = parse_for_int_column(value) value.nil? ? 'NULL' : value.to_s end |