Class: MethodizeString

Inherits:
Object
  • Object
show all
Defined in:
lib/methodize_string/version.rb,
lib/methodize_string/methodize_string.rb

Constant Summary collapse

VERSION =
'0.0.3'

Class Method Summary collapse

Class Method Details

.methodize(str, kwargs = {}) ⇒ Object



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)

  # Chop off a reasonable part of the string to methodize.
  tmp = str.squish.split(/(\n|\(|\[|\.\s*\S+|\/)$/)[0].strip

  # TODO: first, second, third...
  if tmp =~ /^[[:space:]+, \s+]*\d+\S+/
    return nil
  else
    if match = tmp.match(/^[[:space:]+, \s+]*(\d+)(?=[[:space:]+, \s+]+|$)/)
    # if match = tmp.match(/^[[:space:]+, \s+]*(\d+|\d+st|\d+nd|\d+rd|\dth)[[:space:]+, \s+]+/)
      if word = NUMBER_MAP[$1.to_i]
        tmp = tmp.sub(/^[[:space:]+, \s+]*\d+/, word)
      else
        nil
      end
    end
  end

  # Other special cases.
  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_')

  # Remove non-ASCII chars and convert to method format.
  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

.methodize!(str, kwargs = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/methodize_string/methodize_string.rb', line 55

def self.methodize!(str, kwargs = {})
  unless tmp = MethodizeString.methodize(str, **kwargs)
    raise ArgumentError, "Unable to convert string to a method string: '#{str}'"
  end

  tmp
end