Class: Malt::Engine::Builder

Inherits:
Abstract show all
Defined in:
lib/malt/engines/builder.rb

Overview

Builder

http://builder.rubyforge.org/

Instance Attribute Summary

Attributes inherited from Abstract

#settings

Instance Method Summary collapse

Methods inherited from Abstract

#cache?, default, #initialize, register, type

Constructor Details

This class inherits a constructor from Malt::Engine::Abstract

Instance Method Details

#create_engine(params = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/malt/engines/builder.rb', line 41

def create_engine(params={})
  opts = engine_options(params)

  #cached(opts) do
    ::Builder::XmlMarkup.new(opts)
  #end
end

#prepare_engine(params = {}, &content) ⇒ Object

Prepare engine for rendering.



28
29
30
31
32
33
34
35
36
# File 'lib/malt/engines/builder.rb', line 28

def prepare_engine(params={}, &content)
  prefix = parameters(params, :prefix)

  if prefix
    prepare_engine_prefix(params, &content)
  else
    prepare_engine_scope(params, &content)
  end
end

#prepare_engine_prefix(params, &content) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/malt/engines/builder.rb', line 50

def prepare_engine_prefix(params, &content)
  prefix, text, file, scope, locals = parameters(params, :prefix, :text, :file, :scope, :locals)

  bind = make_binding(scope, locals, &content)

  #scope, locals = split_data(data)

  #scope  ||= Object.new
  #locals ||= {}

  engine = create_engine(params)

  code = %{
    lambda do |#{prefix}|
      #{text}
    end
  }

  eval(code, bind, file || '(builder)').call(engine)
end

#prepare_engine_scope(params, &content) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/malt/engines/builder.rb', line 75

def prepare_engine_scope(params, &content)
  text, file, scope, locals = parameters(params, :text, :file, :scope, :locals)

  scope, locals = make_external(scope, locals, &content)

  engine = create_engine

  locals.each do |k,v|
    next if k.to_sym == :target
    engine.instance_eval("@#{k} = v")
  end

  unless scope.respond_to?(:to_struct)
    scope.instance_variables.each do |k|
      next if k == "@target"
      v = scope.instance_variable_get(k)
      engine.instance_eval("#{k} = v") 
    end
  end

  engine.instance_eval(text, file || '(builder)')

  engine.target!
end

#render(params = {}, &content) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/malt/engines/builder.rb', line 14

def render(params={}, &content)
  into = parameters(params, :to) || :html

  case into
  when :xml, :html, :xhtml
    prepare_engine(params, &content) #.target!
  else
    super(params, &content)
  end
end