Class: Serve::DynamicHandler::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/serve/handlers/dynamic_handler.rb

Overview

:nodoc:

Constant Summary collapse

HTML_ESCAPE =

View helper methods

{ '&' => '&amp;',  '>' => '&gt;',   '<' => '&lt;', '"' => '&quot;' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path, request, response) ⇒ Context

Returns a new instance of Context.



99
100
101
102
# File 'lib/serve/handlers/dynamic_handler.rb', line 99

def initialize(root_path, request, response)
  @root_path, @request, @response = root_path, request, response
  @content = ''
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



96
97
98
# File 'lib/serve/handlers/dynamic_handler.rb', line 96

def content
  @content
end

#parserObject

Returns the value of attribute parser.



96
97
98
# File 'lib/serve/handlers/dynamic_handler.rb', line 96

def parser
  @parser
end

#requestObject (readonly)

Returns the value of attribute request.



97
98
99
# File 'lib/serve/handlers/dynamic_handler.rb', line 97

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



97
98
99
# File 'lib/serve/handlers/dynamic_handler.rb', line 97

def response
  @response
end

Instance Method Details

#_erboutObject



104
105
106
# File 'lib/serve/handlers/dynamic_handler.rb', line 104

def _erbout
  @erbout
end

#capture_erb(&block) ⇒ Object

This is extracted from Rails



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/serve/handlers/dynamic_handler.rb', line 109

def capture_erb(&block)
  buffer = _erbout
  pos = buffer.length
  block.call
  
  # extract the block 
  data = buffer[pos..-1]
  
  # replace it in the original with empty string
  buffer[pos..-1] = ''
  
  data
end

#content_for(symbol, &block) ⇒ Object

Content_for methods



125
126
127
128
129
130
131
# File 'lib/serve/handlers/dynamic_handler.rb', line 125

def content_for(symbol, &block)
  if @haml_buffer
    set_content_for(symbol, capture_haml(&block))
  else
    set_content_for(symbol, capture_erb(&block))
  end
end

#content_for?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/serve/handlers/dynamic_handler.rb', line 133

def content_for?(symbol)
  !(get_content_for(symbol)).nil?
end

#get_content_for(symbol = :content) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/serve/handlers/dynamic_handler.rb', line 137

def get_content_for(symbol = :content)
  if symbol.to_s.intern == :content
    @content
  else
    instance_variable_get("@content_for_#{symbol}")
  end
end

#html_escape(s) ⇒ Object Also known as: h



148
149
150
# File 'lib/serve/handlers/dynamic_handler.rb', line 148

def html_escape(s)
  s.to_s.gsub(/[&"><]/) { |special| HTML_ESCAPE[special] }
end

#paramsObject



157
158
159
# File 'lib/serve/handlers/dynamic_handler.rb', line 157

def params
  request.params
end

#render(options) ⇒ Object

Render methods



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/serve/handlers/dynamic_handler.rb', line 163

def render(options)
  partial = options.delete(:partial)
  template = options.delete(:template)
  case
  when partial
    render_partial(partial)
  when template
    render_template(template)
  else
    raise "render options not supported #{options.inspect}"
  end
end

#render_partial(partial) ⇒ Object



176
177
178
# File 'lib/serve/handlers/dynamic_handler.rb', line 176

def render_partial(partial)
  render_template(partial, :partial => true)
end

#render_template(template, options = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/serve/handlers/dynamic_handler.rb', line 180

def render_template(template, options={})
  path = File.dirname(parser.script_filename)
  if template =~ %r{^/}
    template = template[1..-1]
    path = @root_path
  end
  filename = template_filename(File.join(path, template), :partial => options.delete(:partial))
  if File.file?(filename)
    parser.parse_file(filename)
  else
    raise "File does not exist #{filename.inspect}"
  end
end

#set_content_for(symbol, value) ⇒ Object



153
154
155
# File 'lib/serve/handlers/dynamic_handler.rb', line 153

def set_content_for(symbol, value)
  instance_variable_set("@content_for_#{symbol}", value)
end