module Eco module API class UseCases class OozeSamples module Helpers module Shortcuts # Offers multiple ways to compare two strings def same_string?(value1, value2, exact: false, mild: false) case when value1.is_a?(String) && value2.is_a?(String) if exact value1 == value2 else v1 = value1.to_s.strip.downcase v2 = value2.to_s.strip.downcase if mild v1 = v1.gsub(/[^a-z ]+/, ' ').gsub(/\s+/, ' ').strip v2 = v2.gsub(/[^a-z ]+/, ' ').gsub(/\s+/, ' ').strip end v1 == v2 end when value1.is_a?(Regexp) && value2.is_a?(String) value2 =~ value1 when value1.is_a?(String) && value2.is_a?(Regexp) value1 =~ value2 else value1 == value2 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 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 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 end end end def object_reference(obj) return "No reference" unless obj "".tap do |ref| case obj when Ecoportal::API::V2::Page::Stage ref << "Stage" when Ecoportal::API::V2::Pages::PageStage ref << "Page (#{obj.id}) (#{object_reference(obj.current_stage)})" 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)}" 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 ref << " '#{obj.name}'" if obj.respond_to?(:name) end end def to_i 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 end end end end end end end