Class: Ate
- Inherits:
-
Object
- Object
- Ate
- Defined in:
- lib/ate.rb
Overview
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
- .build_proc(lines, vars) ⇒ Object
- .declaring_local_variables(vars) ⇒ Object
- .parse(template, vars = {}) ⇒ Object
- .parsing_lines(lines) ⇒ Object
- .render ⇒ Object
Class Method Details
.build_proc(lines, vars) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/ate.rb', line 33 def build_proc(lines, vars) @output = "Proc.new do \n output = \"\" \n " declaring_local_variables(vars) parsing_lines(lines) @output << "output \n end" end |
.declaring_local_variables(vars) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/ate.rb', line 40 def declaring_local_variables(vars) vars.each do |x, y| value = y.is_a?(String) ? "\"#{y}\"" : y @output << "#{x} = #{value}\n" end end |
.parse(template, vars = {}) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/ate.rb', line 24 def parse(template, vars = {}) context = vars.fetch(:context, self) lines = template.end_with?(".ate") ? File.read(template) : template lines = lines.split("\n") built = build_proc(lines, vars) @parsed = context.instance_eval(built) self end |
.parsing_lines(lines) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/ate.rb', line 47 def parsing_lines(lines) lines.each do |line| if line =~ /^\s*(%)(.*?)$/ @output << "#{line.gsub(/^\s*%(.*?)$/, '\1') } \n" else @output << "output << %Q|#{line.gsub(/\{\{([^\r\n]*)\}\}/, '#{\1}')}\n| \n " end end end |
.render ⇒ Object
57 58 59 |
# File 'lib/ate.rb', line 57 def render @parsed.call end |