Class: Brochure::Context

Inherits:
Object
  • Object
show all
Includes:
Tilt::CompileSite
Defined in:
lib/brochure/context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, template, env, assigns = {}) ⇒ Context

Returns a new instance of Context.



13
14
15
16
17
18
19
# File 'lib/brochure/context.rb', line 13

def initialize(application, template, env, assigns = {})
  @application = application
  @template = template
  @env = env

  load_assigns(assigns)
end

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



11
12
13
# File 'lib/brochure/context.rb', line 11

def application
  @application
end

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/brochure/context.rb', line 11

def env
  @env
end

#templateObject (readonly)

Returns the value of attribute template.



11
12
13
# File 'lib/brochure/context.rb', line 11

def template
  @template
end

Class Method Details

.for(helpers) ⇒ Object



5
6
7
8
9
# File 'lib/brochure/context.rb', line 5

def self.for(helpers)
  context = Class.new(self)
  context.send(:include, *helpers) if helpers.any?
  context
end

Instance Method Details

#captureObject



51
52
53
54
55
56
57
58
# File 'lib/brochure/context.rb', line 51

def capture
  buf = ""
  old_buf, @_out_buf = @_out_buf, buf
  yield
  buf
ensure
  @_out_buf = old_buf
end

#h(html) ⇒ Object



31
32
33
# File 'lib/brochure/context.rb', line 31

def h(html)
  Rack::Utils.escape_html(html)
end

#load_assigns(assigns) ⇒ Object



21
22
23
24
25
# File 'lib/brochure/context.rb', line 21

def load_assigns(assigns)
  assigns.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end


47
48
49
# File 'lib/brochure/context.rb', line 47

def print(str)
  @_out_buf << str
end

#render(logical_path, locals = {}, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brochure/context.rb', line 35

def render(logical_path, locals = {}, &block)
  if partial = application.find_partial(logical_path, template.format_extension)
    if block_given?
      print partial.render(env, locals) { capture(&block) }
    else
      partial.render(env, locals)
    end
  else
    raise TemplateNotFound, "no such template '#{logical_path}'"
  end
end

#requestObject



27
28
29
# File 'lib/brochure/context.rb', line 27

def request
  @request ||= Rack::Request.new(env)
end