4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/methodize_string/methodize_string.rb', line 4
def self.methodize(str, kwargs = {})
return nil if str.blank?
args = {
transliterate: true
}.merge(kwargs)
tmp = str.squish.split(/(\n|\(|\[|\.\s*\S+|\/)$/)[0].strip
if tmp =~ /^[[:space:]+, \s+]*\d+\S+/
return nil
else
if match = tmp.match(/^[[:space:]+, \s+]*(\d+)(?=[[:space:]+, \s+]+|$)/)
if word = NUMBER_MAP[$1.to_i]
tmp = tmp.sub(/^[[:space:]+, \s+]*\d+/, word)
else
nil
end
end
end
tmp = tmp.gsub(/(\s+&{1,2}\s+)/, '_and_')
tmp = tmp.gsub(/(\s+\|\|\s+)/, '_or_')
tmp = tmp.gsub(/(\s+%\s+)/, '_percent_')
tmp = tmp.gsub(/(\s+\+\s+)/, '_plus_')
tmp = tmp.chars.map do |c|
c if c =~ /^[\p{L}\p{M}\p{Nd}\s]+$/
end.compact.join.mb_chars.downcase.gsub(/[[:space:],\s]+/, '_').squish.to_s
if tmp.present?
if args[:transliterate]
tmp = I18n.transliterate(tmp)
if tmp =~ /\?/
nil
else
tmp
end
else
tmp
end
else
nil
end
end
|