Sha256: 51ac71ea68de4a60d15eed7cf955b39fd349257cef4d0e1a8d0bb6eeaab40df9

Contents?: true

Size: 996 Bytes

Versions: 5

Compression:

Stored size: 996 Bytes

Contents

module Innate
  module View
    module Etanni
      def self.call(action, string)
        etanni = View.compile(string) do |str|
          filename = action.view || action.method
          Innate::Etanni.new(str, filename)
        end
        etanni.result(action.instance)
      end
    end
  end

  class Etanni
    SEPARATOR = "E69t116A65n110N78i105S83e101P80a97R82a97T84o111R82"
    CHOMP = "<<#{SEPARATOR}.chomp!"
    START = "\n_out_ << #{CHOMP}\n"
    STOP = "\n#{SEPARATOR}\n"
    REPLACEMENT = "#{STOP}\\1#{START}"

    def initialize(template, filename = '<Etanni>')
      @template = template
      @filename = filename
      compile
    end

    def compile(filename = @filename)
      temp = @template.strip
      temp.gsub!(/<\?r\s+(.*?)\s+\?>/m, REPLACEMENT)
      @compiled = eval("Proc.new{ _out_ = [#{CHOMP}]\n#{temp}#{STOP}_out_.join }",
        nil, @filename)
    end

    def result(instance, filename = @filename)
      instance.instance_eval(&@compiled)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
innate-2023.01.06 lib/innate/view/etanni.rb
innate-2015.10.28 lib/innate/view/etanni.rb
innate-2013.02.21 lib/innate/view/etanni.rb
innate-2013.02 lib/innate/view/etanni.rb
innate-2012.12 lib/innate/view/etanni.rb