Sha256: 0761a1828e2582395fc5b55165f6b4dfca6be52f1849f9cabcb64a08289f1cc9

Contents?: true

Size: 992 Bytes

Versions: 9

Compression:

Stored size: 992 Bytes

Contents

require "erb"

module MVCLI
  class ERB
    def initialize
      @compiler = ::ERB::Compiler.new("<>")
      @compiler.pre_cmd = [""]
      @compiler.put_cmd = "@_erbout <<"
      @compiler.insert_cmd = "@_erbout <<"
      @compiler.post_cmd = ["nil"]
    end

    def compile(string, filename = '(erb)')
      code, enc = @compiler.compile string
      Template.new code, enc, filename
    end

    class Context < BasicObject
      attr_reader :this
      def initialize(this)
        @this = this
      end
    end

    class Template
      def initialize(code, enc, filename)
        @code, @enc, @filename = code, enc, filename
      end

      def call(this, output)
        context = Context.new this
        binding = context.instance_eval do
          @_erbout = output
          Kernel.binding
        end
        eval @code, binding, @filename, 1
      end

      def to_proc
        proc do |context, output|
          call context, output
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
mvcli-0.1.0 lib/mvcli/erb.rb
mvcli-0.0.16 lib/mvcli/erb.rb
mvcli-0.0.14 lib/mvcli/erb.rb
mvcli-0.0.13 lib/mvcli/erb.rb
mvcli-0.0.12 lib/mvcli/erb.rb
mvcli-0.0.11 lib/mvcli/erb.rb
mvcli-0.0.10 lib/mvcli/erb.rb
mvcli-0.0.9 lib/mvcli/erb.rb
mvcli-0.0.8 lib/mvcli/erb.rb