Module: Erubis::RubyEvaluator

Includes:
Evaluator
Included in:
Eruby, OptimizedEruby, PI::Eruby
Defined in:
lib/erubis/evaluator.rb

Overview

evaluator for Ruby

Instance Attribute Summary

Attributes included from Evaluator

#filename, #src

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Evaluator

#init_evaluator

Class Method Details

.supported_propertiesObject

:nodoc:



47
48
49
50
# File 'lib/erubis/evaluator.rb', line 47

def self.supported_properties    # :nodoc:
  list = Evaluator.supported_properties
  return list
end

Instance Method Details

#def_method(object, method_name, filename = nil) ⇒ Object

if object is an Class or Module then define instance method to it, else define singleton method to it.



79
80
81
82
# File 'lib/erubis/evaluator.rb', line 79

def def_method(object, method_name, filename=nil)
  m = object.is_a?(Module) ? :module_eval : :instance_eval
  object.__send__(m, "def #{method_name}; #{@src}; end", filename || @filename || '(erubis)')
end

#evaluate(_context = Context.new) ⇒ Object

invoke context.instance_eval(@src)



69
70
71
72
73
74
75
# File 'lib/erubis/evaluator.rb', line 69

def evaluate(_context=Context.new)
  _context = Context.new(_context) if _context.is_a?(Hash)
  #return _context.instance_eval(@src, @filename || '(erubis)')
  #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)')
  @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)')
  return _context.instance_eval(&@_proc)
end

#result(_binding_or_hash = TOPLEVEL_BINDING) ⇒ Object

eval(@src) with binding object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/erubis/evaluator.rb', line 53

def result(_binding_or_hash=TOPLEVEL_BINDING)
  _arg = _binding_or_hash
  if _arg.is_a?(Hash)
    _b = binding()
    eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join, _b
  elsif _arg.is_a?(Binding)
    _b = _arg
  elsif _arg.nil?
    _b = binding()
  else
    raise ArgumentError.new("#{self.class.name}#result(): argument should be Binding or Hash but passed #{_arg.class.name} object.")
  end
  return eval(@src, _b, (@filename || '(erubis'))
end