Class: MODL::Parser::StandardMethods

Inherits:
Object
  • Object
show all
Defined in:
lib/modl/parser/modl_method.rb

Constant Summary collapse

@@mthd_names =
%w(u d i s e r t p upcase downcase initcap sentence urlencode replace trim punydecode)

Class Method Summary collapse

Class Method Details

.extract_params(str) ⇒ Object

Extract the method parameter



158
159
160
161
# File 'lib/modl/parser/modl_method.rb', line 158

def self.extract_params(str)
  # should be of the form .r(s1,s2)
  Sutil.between(str, '<', '>')
end

.get_subst_parts(s) ⇒ Object

Extract the method parameters



149
150
151
152
153
154
155
# File 'lib/modl/parser/modl_method.rb', line 149

def self.get_subst_parts(s)
  # should be of the form .r(s1,s2)
  result = extract_params(s).split(',')
  result[0] = '' if result.length.zero? || result[0].nil?
  result[1] = '' if result.length == 1 || result[1].nil?
  result
end

.run_method(mthd, str) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/modl/parser/modl_method.rb', line 119

def self.run_method(mthd, str)
  m = mthd.match(/\w*/)[0]
  case m
  when 'u', 'upcase'
    str.upcase
  when 'd', 'downcase'
    str.downcase
  when 'i', 'initcap'
    str.split.map(&:capitalize) * ' '
  when 's', 'sentence'
    split = str.split
    split[0].capitalize!
    split.join(' ')
  when 'e', 'urlencode'
    CGI.escape(str)
  when 'r', 'replace'
    s1, s2 = get_subst_parts(mthd)
    str.gsub(s1, s2)
  when 't', 'trim'
    s1 = extract_params mthd
    i = str.index(s1)
    Sutil.head(str, i)
  when 'p', 'punydecode'
    Punycode.decode(str)
  else
    str.nil? ? '.' + mthd : str.to_s + '.' + mthd
  end
end

.valid_method?(mthd) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
# File 'lib/modl/parser/modl_method.rb', line 163

def self.valid_method?(mthd)
  m = mthd
  if m.include?('<')
    m = Sutil.until(m, '<')
  end
  return @@mthd_names.include?(m)
end