Class: Brochure::Context

Inherits:
Object
  • Object
show all
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.



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

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.



9
10
11
# File 'lib/brochure/context.rb', line 9

def application
  @application
end

#envObject (readonly)

Returns the value of attribute env.



9
10
11
# File 'lib/brochure/context.rb', line 9

def env
  @env
end

#templateObject (readonly)

Returns the value of attribute template.



9
10
11
# File 'lib/brochure/context.rb', line 9

def template
  @template
end

Class Method Details

.for(helpers) ⇒ Object



3
4
5
6
7
# File 'lib/brochure/context.rb', line 3

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

Instance Method Details

#capture(&block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/brochure/context.rb', line 59

def capture(&block)
  if respond_to?(method = "capture_#{engine_name}")
    send(method, &block)
  elsif @_out_buf
    begin
      buf = ""
      old_buf, @_out_buf = @_out_buf, buf
      yield
      buf
    ensure
      @_out_buf = old_buf
    end
  else
    raise CaptureNotSupported, "no capture support for #{engine_name} templates"
  end
end

#concat(str) ⇒ Object



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

def concat(str)
  if respond_to?(method = "#{engine_name}_concat")
    send(method, str)
  elsif @_out_buf
    @_out_buf << str
  else
    raise CaptureNotSupported, "no capture support for #{engine_name} templates"
  end
end

#engine_nameObject



45
46
47
# File 'lib/brochure/context.rb', line 45

def engine_name
  template.engine_extension[1..-1]
end

#h(html) ⇒ Object



29
30
31
# File 'lib/brochure/context.rb', line 29

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

#load_assigns(assigns) ⇒ Object



19
20
21
22
23
# File 'lib/brochure/context.rb', line 19

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

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



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

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

#requestObject



25
26
27
# File 'lib/brochure/context.rb', line 25

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