Class: MODL::Parser::StandardMethods
- Inherits:
-
Object
- Object
- MODL::Parser::StandardMethods
- 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
-
.extract_params(str) ⇒ Object
Extract the method parameter.
-
.get_subst_parts(s) ⇒ Object
Extract the method parameters.
- .run_method(mthd, str) ⇒ Object
- .valid_method?(mthd) ⇒ Boolean
Class Method Details
.extract_params(str) ⇒ Object
Extract the method parameter
154 155 156 157 |
# File 'lib/modl/parser/modl_method.rb', line 154 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
145 146 147 148 149 150 151 |
# File 'lib/modl/parser/modl_method.rb', line 145 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
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/modl/parser/modl_method.rb', line 115 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
159 160 161 162 163 164 165 |
# File 'lib/modl/parser/modl_method.rb', line 159 def self.valid_method?(mthd) m = mthd if m.include?('<') m = Sutil.until(m, '<') end return @@mthd_names.include?(m) end |