lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb in eco-helpers-2.6.4 vs lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb in eco-helpers-2.7.0

- old
+ new

@@ -9,11 +9,11 @@ @non_letters_regex ||= /[^a-z ]+/ end # Matches anything between two consecutive (), inclusive def bracked_regex - @bracked_regex ||= /(?<bracked>\([^\)]+?\))/ + @bracked_regex ||= /(?<bracked>\([^\)]+?\))/ # rubocop:disable Style/RedundantRegexpEscape end # It always downcase, trim and remove double spaces. # @param ignore Boolean, Regexp] ingored when `exact` is `true` # * when `false`: it does not do anything additional @@ -22,85 +22,93 @@ # * when `Array`: reduces `str` processing `ignore` in order. # * when `true` (or otherwise): it removes all non a-zA-Z characters but blank spaces. def simplify_string(str, ignore: false) str = str.to_s.strip.downcase.gsub(/\s+/, ' ') return str unless ignore + sub = non_letters_regex case ignore - when Regexp; sub = ignore - when String; sub = /[#{ignore}]+/ + when Regexp then sub = ignore + when String then sub = /[#{ignore}]+/ when Array return ignore.reduce(str) do |out, sub| simplify_string(out, ignore: sub) end end str.gsub(sub, '').gsub(/\s+/, ' ').strip end # Offers multiple simplification methods to compare two strings # @note only one of the values can be a Regexp - # @param value1 [String, Regexp, Nil] - # @param value2 [String, Regexp, Nil] + # @param value_1 [String, Regexp, Nil] + # @param value_2 [String, Regexp, Nil] # @param exact [Boolean] # * when `true`: requiring the values to be exactly the same # * otherwise (`false`): compares in downcase, with no extra spaces # @param mild [Boolean] only a-z comparison # @param ignore [see @simplify_string] - def same_string?(value1, value2, exact: false, mild: false, ignore: false) - return true if value1.to_s.strip.empty? && value2.to_s.strip.empty? - return false if value1.to_s.strip.empty? || value2.to_s.strip.empty? - val1, val2 = value1, value2 + def same_string?(value_1, value_2, exact: false, mild: false, ignore: false) # rubocop:disable Metrics/AbcSize + return true if value_1.to_s.strip.empty? && value_2.to_s.strip.empty? + return false if value_1.to_s.strip.empty? || value_2.to_s.strip.empty? + + val_1, val_2 = value_1, value_2 # rubocop:disable Style/ParallelAssignment + unless exact - if val1.is_a?(String) - val1 = simplify_string(val1, ignore: ignore) if ignore - val1 = simplify_string(val1, ignore: non_letters_regex) if mild + if val_1.is_a?(String) + val_1 = simplify_string(val_1, ignore: ignore) if ignore + val_1 = simplify_string(val_1, ignore: non_letters_regex) if mild end - if val2.is_a?(String) - val2 = simplify_string(val2, ignore: ignore) if ignore - val2 = simplify_string(val2, ignore: non_letters_regex) if mild + if val_2.is_a?(String) + val_2 = simplify_string(val_2, ignore: ignore) if ignore + val_2 = simplify_string(val_2, ignore: non_letters_regex) if mild end end - case - when val1.is_a?(String) && val2.is_a?(String) - val1 == val2 - when val1.is_a?(Regexp) && val2.is_a?(String) - val2 =~ val1 - when val1.is_a?(String) && val2.is_a?(Regexp) - val1 =~ val2 + + if val_1.is_a?(String) && val_2.is_a?(String) + val_1 == val_2 + elsif val_1.is_a?(Regexp) && val_2.is_a?(String) + val_2 =~ val_1 + elsif val_1.is_a?(String) && val_2.is_a?(Regexp) + val_1 =~ val_2 else - #val1 == val2 - raise "Expected at least one String, and either a String or Regex. Given: (1: #{val1.class}) and (2: #{val2.class})" + #val_1 == val_2 + msg = "Expected at least one String, and either a String or Regex. " + msg << "Given: (1: #{val_1.class}) and (2: #{val_2.class})" + raise ArgumentError, msg end end def titleize(str) return nil unless str return str if str.strip.empty? + str.split(/\s+/).map do |part| part[0] = part[0].upcase - part[1..-1] = part[1..-1].downcase + part[1..-1] = part[1..].downcase part end.join(" ") end def normalize_string(str) return nil unless str - str.gsub(/[^[:print:]]/, '') - .gsub(/[[:space:]]+/, ' ') - .gsub(/[[:space:]]$/, '') - .gsub(/[-\u2011\u2012\u2013]/, '-').yield_self do |str| - str = yield(str) if block_given? - str - end + + str.gsub(/[^[:print:]]/, ''). + gsub(/[[:space:]]+/, ' '). + gsub(/[[:space:]]$/, ''). + gsub(/[-\u2011\u2012\u2013]/, '-').then do |aux| + aux = yield(aux) if block_given? + aux + end end def clean_question(str) return nil unless str - normalize_string(str) do |str| - str.gsub(/\r\n/, ' ').yield_self do |str| - str = yield(str) if block_given? - str + + normalize_string(str) do |x| + x.gsub(/\r\n/, ' ').then do |aux| + aux = yield(aux) if block_given? + aux end end end def object_reference(obj) @@ -114,11 +122,11 @@ when Ecoportal::API::V2::Page ref << "Page" when Ecoportal::API::V2::Page::Section ref << "Section '#{obj.heading || "(unnamed)"}'" when Ecoportal::API::V2::Page::Component - ref << "Component '#{obj.label || "(unnamed of type '#{obj.type}')"}' in #{object_reference(obj.section)}" + ref << "Component '#{obj.label || "(unnamed of type '#{obj.type}')"}' in #{object_reference(obj.section)}" # rubocop:disable Layout/LineLength when Ecoportal::API::V2::Page::Force ref << "Force '#{obj.name}'" when Ecoportal::API::V2::Page::Force::Binding ref << "Binding '#{obj.name}' in #{object_reference(obj.force)}" end @@ -129,15 +137,13 @@ def to_i(value) Float(value).to_i end # https://stackoverflow.com/a/5661695/4352306 - def is_number?(value) - begin - true if Float(value) - rescue ArgumentError => e - false - end + def is_number?(value) # rubocop:disable Naming/PredicateName + true if Float(value) + rescue ArgumentError + false end end end end end