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

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

Instance Method Summary collapse

Instance Method Details

#clean_question(str) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 47

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

#normalize_string(str) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 36

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

#object_reference(obj) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 57

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

#same_string?(value1, value2, exact: false) ⇒ Boolean

Offers multiple ways to compare two strings

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 9

def same_string?(value1, value2, exact: false)
  case
  when value1.is_a?(String) && value2.is_a?(String)
    if exact
      value1 == value2
    else
      value1.to_s.strip.downcase == value2.to_s.strip.downcase
    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

#titleize(str) ⇒ Object



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

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