Module: DR::Eruby::ClassHelpers

Included in:
DR::Eruby
Defined in:
lib/dr/base/eruby.rb

Instance Method Summary collapse

Instance Method Details

#evaluate(_proc, context: Context.new, vars: nil, &b) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 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) ⇒ Object



133
134
135
136
# 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) ⇒ Object



138
139
140
# 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