Module: Eco::API::UseCases::OozeSamples::Helpers::Shortcuts

Included in:
Eco::API::UseCases::OozeSamples::Helpers, Eco::API::UseCases::OozeSamples::HelpersMigration::TypedFieldsPairing
Defined in:
lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb

Instance Method Summary collapse

Instance Method Details

#bracked_regexObject

Matches anything between two consecutive (), inclusive



13
14
15
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 13

def bracked_regex
  @bracked_regex ||= /(?<bracked>\([^\)]+?\))/ # rubocop:disable Style/RedundantRegexpEscape
end

#clean_question(str) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 103

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

Returns:

  • (Boolean)


142
143
144
145
146
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 142

def is_number?(value) # rubocop:disable Naming/PredicateName
  true if Float(value)
rescue ArgumentError
  false
end

#non_letters_regexObject

Basic simplification pattern (matches all but a-z and blank space)



8
9
10
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 8

def non_letters_regex
  @non_letters_regex ||= /[^a-z ]+/
end

#normalize_string(str) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 91

def normalize_string(str)
  return nil unless str

  str.gsub(/[^[:print:]]/, '').
    gsub(/[[:space:]]+/, ' ').
    gsub(/[[:space:]]$/, '').
    gsub(/[-\u2011\u2012\u2013]/, '-').then do |aux|
      aux = yield(aux) if block_given?
      aux
    end
end

#object_reference(obj) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 114

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)}" # 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
    ref << " '#{obj.name}'" if obj.respond_to?(:name)
  end
end

#same_string?(value_1, value_2, exact: false, mild: false, ignore: false) ⇒ Boolean

Note:

only one of the values can be a Regexp

Offers multiple simplification methods to compare two strings

Parameters:

  • value_1 (String, Regexp, Nil)
  • value_2 (String, Regexp, Nil)
  • exact (Boolean) (defaults to: false)
    • when true: requiring the values to be exactly the same
    • otherwise (false): compares in downcase, with no extra spaces
  • mild (Boolean) (defaults to: false)

    only a-z comparison

  • ignore (see @simplify_string) (defaults to: false)

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 49

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 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
    #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

#simplify_string(str, ignore: false) ⇒ Object

It always downcase, trim and remove double spaces.

Parameters:

  • ignore (defaults to: false)

    Boolean, Regexp] ingored when exact is true

    • when false: it does not do anything additional
    • when Regex: it removes all characters that match the expression.
    • when String: each character listed is removed.
    • when Array: reduces str processing ignore in order.
    • when true (or otherwise): it removes all non a-zA-Z characters but blank spaces.


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 24

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|
      simplify_string(out, ignore: sub)
    end
  end
  str.gsub(sub, '').gsub(/\s+/, ' ').strip
end

#titleize(str) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 80

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..].downcase
    part
  end.join(" ")
end

#to_i(value) ⇒ Object



137
138
139
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 137

def to_i(value)
  Float(value).to_i
end