Module: Eco::API::UseCases::GraphQL::Helpers::Pages::Shortcuts
- Included in:
- Copying, TypedFieldsPairing, Samples::Pages::Register::Base
- Defined in:
- lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb
Overview
String simplification / comparison helpers, ported from OozeSamples::Helpers::Shortcuts. Everything here is pure except #object_reference, which is re-expressed with duck-typing (no Ecoportal::API::V2 constants) so it describes GraphQL page/stage/section/field/force models.
Instance Method Summary collapse
-
#bracked_regex ⇒ Object
Matches anything between two consecutive (), inclusive.
- #clean_question(str) ⇒ Object
-
#is_number?(value) ⇒ Boolean
https://stackoverflow.com/a/5661695/4352306 Kept as
is_number?for naming parity with OozeSamples::Helpers::Shortcuts. -
#non_letters_regex ⇒ Object
Basic simplification pattern (matches all but a-z and blank space).
- #normalize_string(str) ⇒ Object
-
#object_reference(obj) ⇒ Object
Human-readable reference for a GraphQL page-model object, for logs/errors.
-
#same_string?(value_1, value_2, exact: false, mild: false, ignore: false) ⇒ Boolean
Offers multiple simplification methods to compare two strings.
-
#simplify_string(str, ignore: false) ⇒ Object
It always downcase, trim and remove double spaces.
- #titleize(str) ⇒ Object
- #to_i(value) ⇒ Object
Instance Method Details
#bracked_regex ⇒ Object
Matches anything between two consecutive (), inclusive
14 15 16 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 14 def bracked_regex @bracked_regex ||= /(?<bracked>\([^\)]+?\))/ # rubocop:disable Style/RedundantRegexpEscape end |
#clean_question(str) ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 97 def clean_question(str) return nil unless str normalize_string(str) do |x| x.gsub("\r\n", ' ').then do |aux| aux = yield(aux) if block_given? aux end end end |
#is_number?(value) ⇒ Boolean
https://stackoverflow.com/a/5661695/4352306
Kept as is_number? for naming parity with OozeSamples::Helpers::Shortcuts.
144 145 146 147 148 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 144 def is_number?(value) # rubocop:disable Naming/PredicatePrefix true if Float(value) rescue ArgumentError, TypeError false end |
#non_letters_regex ⇒ Object
Basic simplification pattern (matches all but a-z and blank space)
9 10 11 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 9 def non_letters_regex @non_letters_regex ||= /[^a-z ]+/ end |
#normalize_string(str) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 85 def normalize_string(str) return nil unless str str.gsub(/[^[:print:]]/, ''). gsub(/[[:space:]]+/, ' '). gsub(/[[:space:]]$/, ''). gsub(/[-‑‒–]/, '-').then do |aux| aux = yield(aux) if block_given? aux end end |
#object_reference(obj) ⇒ Object
Human-readable reference for a GraphQL page-model object, for logs/errors.
Re-expressed with duck-typing (was a V2 case/when on Page/Stage/Section/Component/
Force/Binding). We inspect the object's shape rather than its class so this works
across Base::/Model:: GraphQL types without coupling to constants that may move.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 113 def object_reference(obj) return 'No reference' unless obj if binding_like?(obj) "Binding '#{safe_name(obj)}' in #{object_reference(obj.force)}" elsif force_like?(obj) "Force '#{safe_name(obj)}'" elsif component_like?(obj) label = obj.respond_to?(:label) ? obj.label : nil type = obj.respond_to?(:type) ? obj.type : nil ref = "Component '#{label || "(unnamed of type '#{type}')"}'" ref += " in #{object_reference(obj.section)}" if obj.respond_to?(:section) && obj.section ref elsif section_like?(obj) heading = obj.respond_to?(:heading) ? obj.heading : nil "Section '#{heading || '(unnamed)'}'" elsif stage_like?(obj) named_ref('Stage', obj) elsif page_like?(obj) named_ref("Page (#{obj.id})", obj) else named_ref('', obj).strip end end |
#same_string?(value_1, value_2, exact: false, mild: false, ignore: false) ⇒ Boolean
only one of the values can be a Regexp
Offers multiple simplification methods to compare two strings
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 43 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 = value_1 val_2 = value_2 unless exact 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 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 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 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 |
#simplify_string(str, ignore: false) ⇒ Object
It always downcase, trim and remove double spaces.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 25 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 then sub = ignore when String then sub = /[#{ignore}]+/ when Array return ignore.reduce(str) do |out, sub_ignore| simplify_string(out, ignore: sub_ignore) end end str.gsub(sub, '').gsub(/\s+/, ' ').strip end |
#titleize(str) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 74 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..] = part[1..].downcase part end.join(' ') end |
#to_i(value) ⇒ Object
138 139 140 |
# File 'lib/eco/api/usecases/graphql/helpers/pages/shortcuts.rb', line 138 def to_i(value) Float(value).to_i end |