module DR::Eruby::ClassHelpers

Public Instance Methods

evaluate(_proc, context: Context.new, vars: nil, &b) click to toggle source
# File lib/dr/base/eruby.rb, line 142
def evaluate(_proc, context: Context.new, vars: nil, &b)
        #we can only pass the block b when we get an UnboundMethod
        if _proc.is_a?(UnboundMethod)
                vars={} if _proc.arity > 0 and vars.nil?
                if !vars.nil?
                        _proc.bind(context).call(vars,&b)
                else
                        _proc.bind(context).call(&b)
                end
        elsif _proc.is_a?(String)
                #in this case we cannot pass vars
                warn "Cannot pass variables when _proc is a String" unless vars.nil?
                context.instance_eval(_proc)
        else
                if context.nil?
                        if !vars.nil?
                                _proc.to_proc.call(vars,&b)
                        else
                                _proc.to_proc(&b)
                        end
                else
                        warn "Cannot pass block in context.instance_eval" unless b.nil?
                        if !vars.nil?
                                context.instance_exec(vars,&_proc)
                        else
                                context.instance_eval(&_proc)
                        end
                end
        end
end
include(template, **opts) click to toggle source
# File lib/dr/base/eruby.rb, line 173
def include(template, **opts)
        file=File.expand_path(template)
        Dir.chdir(File.dirname(file)) do |cwd|
                erb = Engine.new(File.read(file))
                #if context is not empty, then we probably want to evaluate
                if opts[:evaluate] or opts[:context]
                        r=erb.evaluate(opts[:context])
                else
                        bind=opts[:bind]||binding
                        r=erb.result(bind)
                end
                #if using erubis, it is better to invoke the template in <%= =%> than
                #to use chomp=true
                r=r.chomp if opts[:chomp]
                return r
        end
end
process_eruby(eruby_src, **opts, &b) click to toggle source
# File lib/dr/base/eruby.rb, line 133
def process_eruby(eruby_src, **opts, &b)
        src=Eruby::Engine.new(eruby_src).src
        process_ruby(src, **opts, &b)
end
process_ruby(src, src_info: nil, **opts, &b) click to toggle source
# File lib/dr/base/eruby.rb, line 138
def process_ruby(src, src_info: nil, **opts, &b)
        Template.new(src, filename: src_info).evaluate(**opts, &b)
end