Class: Serve::DynamicHandler::Context
- Inherits:
-
Object
- Object
- Serve::DynamicHandler::Context
- Defined in:
- lib/serve/handlers/dynamic_handler.rb
Overview
:nodoc:
Constant Summary collapse
- HTML_ESCAPE =
View helper methods
{ '&' => '&', '>' => '>', '<' => '<', '"' => '"' }
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #_erbout ⇒ Object
-
#capture_erb(&block) ⇒ Object
This is extracted from Rails.
-
#content_for(symbol, &block) ⇒ Object
Content_for methods.
- #content_for?(symbol) ⇒ Boolean
- #get_content_for(symbol = :content) ⇒ Object
- #html_escape(s) ⇒ Object (also: #h)
-
#initialize(root_path, request, response) ⇒ Context
constructor
A new instance of Context.
- #params ⇒ Object
-
#render(options) ⇒ Object
Render methods.
- #render_partial(partial) ⇒ Object
- #render_template(template, options = {}) ⇒ Object
- #set_content_for(symbol, value) ⇒ Object
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
#content ⇒ Object
Returns the value of attribute content.
96 97 98 |
# File 'lib/serve/handlers/dynamic_handler.rb', line 96 def content @content end |
#parser ⇒ Object
Returns the value of attribute parser.
96 97 98 |
# File 'lib/serve/handlers/dynamic_handler.rb', line 96 def parser @parser end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
97 98 99 |
# File 'lib/serve/handlers/dynamic_handler.rb', line 97 def request @request end |
#response ⇒ Object (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
#_erbout ⇒ Object
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
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 |
#params ⇒ Object
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() partial = .delete(:partial) template = .delete(:template) case when partial render_partial(partial) when template render_template(template) else raise "render options not supported #{.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, ={}) 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 => .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 |