From d9b4779559c8f24aadb1cd75ca19e7d234d59dd1 Mon Sep 17 00:00:00 2001 From: Benjamin Scott Date: Fri, 18 Oct 2024 15:08:06 -0600 Subject: [PATCH] refactor: str_is_integer? method --- lib/glare/util.rb | 15 +++++++++++++++ lib/glare/ux_metrics.rb | 11 ++++++++++- sig/glare/util.rbs | 5 +++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 lib/glare/util.rb create mode 100644 sig/glare/util.rbs diff --git a/lib/glare/util.rb b/lib/glare/util.rb new file mode 100644 index 0000000..97f05d6 --- /dev/null +++ b/lib/glare/util.rb @@ -0,0 +1,15 @@ +module Glare + class Util + def self.str_is_integer?(str) + [ # In descending order of likeliness: + /^[-+]?[1-9]([0-9]*)?$/, # decimal + /^0[0-7]+$/, # octal + /^0x[0-9A-Fa-f]+$/, # hexadecimal + /^0b[01]+$/ # binary + ].each do |match_pattern| + return true if str =~ match_pattern + end + false + end + end +end \ No newline at end of file diff --git a/lib/glare/ux_metrics.rb b/lib/glare/ux_metrics.rb index 0178118..ee44833 100644 --- a/lib/glare/ux_metrics.rb +++ b/lib/glare/ux_metrics.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "glare/util" + module Glare module UxMetrics class Error < StandardError; end @@ -21,7 +23,8 @@ def valid? return false unless missing_attributes.empty? return false unless choices.values.all? do |v| - return v.to_i.to_s == v || v.to_f.to_s == v if v.is_a?(String) + return Glare::Util.str_is_integer?(v) if v.is_a?(String) + true end @@ -598,3 +601,9 @@ def in_hotspot? end end end + +class String + def integer? + + end +end \ No newline at end of file diff --git a/sig/glare/util.rbs b/sig/glare/util.rbs new file mode 100644 index 0000000..5ea3cf7 --- /dev/null +++ b/sig/glare/util.rbs @@ -0,0 +1,5 @@ +module Glare + class Util + def self.str_is_integer?: (String) -> bool + end +end \ No newline at end of file