Class: ERB::OpenTemplate

Inherits:
Object show all
Defined in:
lib/standard/facets/erb.rb

Overview

OpenTemplate

The Erb OpenTemplate provides a quick and convenient way to create a clean rendering space with the desired responses.

Ruby 1.8.6 or less can’t handle object scope methods with blocks.

TODO: This might make a good addon library. Just add require ‘erb’ to the erb_result method? Call it OpenResponse?

Instance Method Summary collapse

Constructor Details

#initialize(*objs_ioc) ⇒ OpenTemplate

Returns a new instance of OpenTemplate.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/standard/facets/erb.rb', line 21

def initialize(*objs_ioc)
  ioc = Hash===objs_ioc.last ? objs_ioc.pop : {}
  objs = objs_ioc

  mods = []

  objs.each do |obj|
    mod = Module.new
    obj.public_methods.each do |m|
      mod.module_eval do
        #define_method(m){ |*a,&b| obj.__send__(m,*a,&b) }
        define_method(m, &obj.method(m).to_proc)
      end
    end
    mods << mod
  end

  mod = Module.new
  ioc.each do |k,v|
    mod.module_eval do
      define_method(k){ v }
    end
  end
  mods << mod

  extend *mods.reverse
end

Instance Method Details

#erb_result(str) ⇒ Object



50
51
52
# File 'lib/standard/facets/erb.rb', line 50

def erb_result(str)
  ERB.new(str).result(binding)
end