Class: Migr8::Util::Template
- Inherits:
-
Object
- Object
- Migr8::Util::Template
- Defined in:
- lib/migr8.rb
Constant Summary collapse
- EMBED_REXP =
/(^[ \t]*)?<%(==?|\#)?(.*?)%>([ \t]*\r?\n)?/m
Instance Attribute Summary collapse
-
#src ⇒ Object
Returns the value of attribute src.
Instance Method Summary collapse
- #convert(input) ⇒ Object
-
#initialize(input = "") ⇒ Template
constructor
A new instance of Template.
- #render(context = {}) ⇒ Object
Constructor Details
#initialize(input = "") ⇒ Template
Returns a new instance of Template.
2229 2230 2231 2232 |
# File 'lib/migr8.rb', line 2229 def initialize(input="") #; [!6z4kp] converts input string into ruby code. self.src = convert(input) end |
Instance Attribute Details
#src ⇒ Object
Returns the value of attribute src.
2234 2235 2236 |
# File 'lib/migr8.rb', line 2234 def src @src end |
Instance Method Details
#convert(input) ⇒ Object
2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 |
# File 'lib/migr8.rb', line 2252 def convert(input) #; [!118pw] converts template string into ruby code. #; [!7ht59] escapes '`' and '\\' characters. src = "_buf = '';" # preamble pos = 0 input.scan(EMBED_REXP) do |lspace, ch, code, rspace| match = Regexp.last_match text = input[pos...match.begin(0)] pos = match.end(0) src << _t(text) #; [!u93y5] wraps expression by 'escape()' when <%= %>. #; [!auj95] leave expression as it is when <%== %>. if ch == '=' # expression (escaping) src << _t(lspace) << " _buf << (escape(#{code})).to_s;" << _t(rspace) elsif ch == '==' # expression (without escaping) src << _t(lspace) << " _buf << (#{code}).to_s;" << _t(rspace) elsif ch == '#' # comment src << _t(lspace) << ("\n" * code.count("\n")) << _t(rspace) else # statement if lspace && rspace src << "#{lspace}#{code}#{rspace};" else src << _t(lspace) << code << ';' << _t(rspace) end end end #; [!b10ns] generates ruby code correctly even when no embedded code. rest = $' || input src << _t(rest) src << "\n_buf.to_s\n" # postamble return src end |
#render(context = {}) ⇒ Object
2241 2242 2243 2244 2245 2246 2247 2248 |
# File 'lib/migr8.rb', line 2241 def render(context={}) #; [!umsfx] takes hash object as context variables. #; [!p0po0] context argument can be null. ctx = TemplateContext.new(context) #; [!48pfc] returns rendered string. #; [!1i0v8] escapes "'" into "''" when '<%= %>', and not when '<%== %>'. return ctx.instance_eval(&@_proc) end |