Class: HammerBuilder::Abstract
- Inherits:
-
Object
- Object
- HammerBuilder::Abstract
- Extended by:
- DynamicClasses
- Defined in:
- lib/hammer_builder/abstract.rb,
lib/hammer_builder/abstract/abstract_tag.rb,
lib/hammer_builder/abstract/abstract_single_tag.rb,
lib/hammer_builder/abstract/abstract_double_tag.rb
Overview
Abstract implementation of Builder
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Object) _current
(also: #current)
current tag being builded.
Class Method Summary (collapse)
-
+ (Object) define_tag(tag)
protected
defines instance method for tag in builder.
Instance Method Summary (collapse)
-
- (Object) cdata(content)
insersts CDATA with content.
-
- (Object) comment(comment)
inserts comment.
-
- (Object) doctype
renders html5 doc type.
-
- (Object) flush
private
flushes open tag.
-
- (Object) go_in(*variables, &block)
(also: #dive)
enables you to evaluate block inside the builder with variables.
-
- (Abstract) initialize
constructor
creates a new builder This is quite expensive, HammerBuilder::Pool should be used.
-
- (Object) join(collection, glue = nil) { ... }
joins and renders collection with glue.
-
- (Object) js(js, options = { })
renders js.
-
- (Object) raw(text)
unescaped text to output.
-
- (Object) render(object, method, *args) { ... }
renders object with method.
-
- (Object) reset
resets the builder to the state after creation - much faster then creating a new one.
-
- (Object) set_variables(instance_variables) { ... }
sets instance variables when block is yielded.
-
- (Object) text(text)
escapes text to output.
-
- (String) to_xhtml
Output.
Methods included from DynamicClasses
create_dynamic_classes, dynamic_classes, extended, inherited
Constructor Details
- (Abstract) initialize
creates a new builder This is quite expensive, HammerBuilder::Pool should be used
57 58 59 60 61 62 63 64 65 |
# File 'lib/hammer_builder/abstract.rb', line 57 def initialize() @_output = "" @_stack = [] @_current = nil # tag classes initialization .each do |klass| instance_variable_set(:@_#{klass}", self.class.dynamic_classes[klass.camelize.to_sym].new(self)) end end |
Instance Attribute Details
- (Object) _current Also known as: current
current tag being builded
50 51 52 |
# File 'lib/hammer_builder/abstract.rb', line 50 def _current @_current end |
Class Method Details
+ (Object) define_tag(tag) (protected)
defines instance method for tag in builder
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/hammer_builder/abstract.rb', line 35 def self.define_tag(tag) tag = tag.to_s class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{tag}(*args, &block) flush @_#{tag}.open(*args, &block) end RUBY self. += [tag] end |
Instance Method Details
- (Object) cdata(content)
insersts CDATA with content
86 87 88 89 |
# File 'lib/hammer_builder/abstract.rb', line 86 def cdata(content) flush @_output << Strings::CDATA_START << content.to_s << Strings::CDATA_END end |
- (Object) comment(comment)
inserts comment
80 81 82 83 |
# File 'lib/hammer_builder/abstract.rb', line 80 def comment(comment) flush @_output << Strings::COMMENT_START << comment.to_s << Strings::COMMENT_END end |
- (Object) doctype
renders html5 doc type
94 95 96 |
# File 'lib/hammer_builder/abstract.rb', line 94 def doctype raw "<!DOCTYPE html>\n" end |
- (Object) flush
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
flushes open tag
150 151 152 153 154 155 |
# File 'lib/hammer_builder/abstract.rb', line 150 def flush if @_current @_current.flush @_current = nil end end |
- (Object) go_in(*variables, &block) Also known as: dive
enables you to evaluate block inside the builder with variables
125 126 127 128 |
# File 'lib/hammer_builder/abstract.rb', line 125 def go_in(*variables, &block) instance_exec *variables, &block self end |
- (Object) join(collection, glue = nil) { ... }
joins and renders collection with glue
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/hammer_builder/abstract.rb', line 183 def join(collection, glue = nil, &it) # TODO as helper? two block method call #join(collection, &item).with(&glue) glue_block = case glue when String lambda { text glue } when Proc glue else lambda { } end collection.each_with_index do |obj, i| glue_block.call() if i > 0 obj.is_a?(Proc) ? obj.call : it.call(obj) end end |
- (Object) js(js, options = { })
renders js
170 171 172 173 |
# File 'lib/hammer_builder/abstract.rb', line 170 def js(js, = { }) use_cdata = .delete(:cdata) || false script({ :type => "text/javascript" }.merge()) { use_cdata ? cdata(js) : text(js) } end |
- (Object) raw(text)
unescaped text to output
74 75 76 77 |
# File 'lib/hammer_builder/abstract.rb', line 74 def raw(text) flush @_output << text.to_s end |
- (Object) render(object, method, *args) { ... }
renders object with method
162 163 164 |
# File 'lib/hammer_builder/abstract.rb', line 162 def render(object, method, *args, &block) object.__send__ method, self, *args, &block end |
- (Object) reset
resets the builder to the state after creation - much faster then creating a new one
99 100 101 102 103 104 |
# File 'lib/hammer_builder/abstract.rb', line 99 def reset flush @_output.clear @_stack.clear self end |
- (Object) set_variables(instance_variables) { ... }
sets instance variables when block is yielded
135 136 137 138 139 140 |
# File 'lib/hammer_builder/abstract.rb', line 135 def set_variables(instance_variables) instance_variables.each { |name, value| instance_variable_set("@#{name}", value) } yield(self) instance_variables.each { |name, _| remove_instance_variable("@#{name}") } self end |
- (Object) text(text)
escapes text to output
68 69 70 71 |
# File 'lib/hammer_builder/abstract.rb', line 68 def text(text) flush @_output << CGI.escapeHTML(text.to_s) end |
- (String) to_xhtml
Output
143 144 145 146 |
# File 'lib/hammer_builder/abstract.rb', line 143 def to_xhtml() flush @_output.clone end |