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



54
55
56
57
58
59
60
61
62
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 54

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

#is_number?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_number?(value)
  begin
    true if Float(value)
  rescue ArgumentError => e
    false
  end
end

#normalize_string(str) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 43

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 64

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

#same_string?(value1, value2, exact: false, mild: 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
25
26
27
28
29
30
31
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 9

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

#titleize(str) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 33

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

#to_iObject



87
88
89
# File 'lib/eco/api/usecases/ooze_samples/helpers/shortcuts.rb', line 87

def to_i
  Float(value).to_i
end