Class: HammerBuilder::Abstract

Inherits:
Object
  • Object
show all
Extended by:
DynamicClasses
Defined in:
lib/hammer_builder/abstract.rb,
lib/hammer_builder/abstract/abstract_tag.rb,
lib/hammer_builder/abstract/abstract_double_tag.rb,
lib/hammer_builder/abstract/abstract_single_tag.rb

Overview

Abstract implementation of Builder

Direct Known Subclasses

Standard

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DynamicClasses

dynamic_classes, extended, inherited

Constructor Details

#initializeAbstract

creates a new builder This is quite expensive, HammerBuilder::Pool should be used



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hammer_builder/abstract.rb', line 78

def initialize()
  @_output  = ""
  @_stack   = []
  @_current = nil

  self.class.strings_injector.inject_to self

  # tag classes initialization
  tags.each do |klass|
    instance_variable_set(:"@_#{klass}", self.class.dynamic_classes[klass.camelize.to_sym].new(self))
  end
end

Instance Attribute Details

#_currentObject Also known as: current

current tag being builded



71
72
73
# File 'lib/hammer_builder/abstract.rb', line 71

def _current
  @_current
end

Class Method Details

.strings_injectorObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hammer_builder/abstract.rb', line 22

def self.strings_injector
  @strings_injector ||= StringsInjector.new do
    add :lt, '<'
    add :gt, '>'
    add :slash_lt, '</'
    add :slash_gt, ' />'
    add :dash, '-'
    add :underscore, '_'
    add :space, ' '
    add :spaces, Array.new(300) { |i| ('  ' * i).freeze }
    add :newline, "\n"
    add :quote, '"'
    add :eql, '='
    add :eql_quote, self[:eql] + self[:quote]
    add :comment_start, '<!--'
    add :comment_end, '-->'
    add :cdata_start, '<![CDATA['
    add :cdata_end, ']]>'
  end
end

Instance Method Details

#cdata(content) ⇒ Object

insersts CDATA with content



110
111
112
113
# File 'lib/hammer_builder/abstract.rb', line 110

def cdata(content)
  flush
  @_output << @_str_cdata_start << content.to_s << @_str_cdata_end
end

#comment(comment) ⇒ Object

inserts comment



104
105
106
107
# File 'lib/hammer_builder/abstract.rb', line 104

def comment(comment)
  flush
  @_output << @_str_comment_start << comment.to_s << @_str_comment_end
end

#flushObject

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



174
175
176
177
178
179
# File 'lib/hammer_builder/abstract.rb', line 174

def flush
  if @_current
    @_current.flush
    @_current = nil
  end
end

#go_in(*variables, &block) ⇒ Object Also known as: dive

enables you to evaluate block inside the builder with variables

Examples:

HammerBuilder::Formatted.new.go_in('asd') do |string|
  div string
end.to_html #=> "<div>asd</div>"


149
150
151
152
# File 'lib/hammer_builder/abstract.rb', line 149

def go_in(*variables, &block)
  instance_exec *variables, &block
  self
end

#html5Object

renders html5 doc type

Examples:

html5 # => <!DOCTYPE html>


118
119
120
# File 'lib/hammer_builder/abstract.rb', line 118

def html5
  raw "<!DOCTYPE html>\n"
end

#join(collection, glue = nil) { ... } ⇒ Object

joins and renders collection with glue

Examples:

join([1, 1.2], lambda { text ', ' }) {|o| text o }        # => "1, 1.2"
join([1, 1.2], ', ') {|o| text o }                        # => "1, 1.2"
join([->{ text 1 }, 1.2], ', ') {|o| text o }             # => "1, 1.2"

Parameters:

  • collection (Array<Proc, Object>)

    of objects or lambdas

  • glue (Proc, String) (defaults to: nil)

    can be String which is rendered with #text or block to render

Yields:

  • how to render objects from collection, Proc in collection does not use this block



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/hammer_builder/abstract.rb', line 210

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

#js(js, options = { }) ⇒ Object

renders js

Examples:

js 'a_js_function();' #=> <script type="text/javascript">a_js_function();</script>

Parameters:

  • options (Hash) (defaults to: { })

    a customizable set of options

Options Hash (options):

  • :cdata (Boolean) — default: false

    should cdata be used?



197
198
199
200
# File 'lib/hammer_builder/abstract.rb', line 197

def js(js, options = { })
  use_cdata = options.delete(:cdata) || false
  script({ :type => "text/javascript" }.merge(options)) { use_cdata ? cdata(js) : text(js) }
end

#raw(text) ⇒ Object

unescaped text to output



98
99
100
101
# File 'lib/hammer_builder/abstract.rb', line 98

def raw(text)
  flush
  @_output << text.to_s
end

#render(object, method, *args) { ... } ⇒ Object Also known as: r

renders object with method

Parameters:

  • object (Object)

    an object to render

  • method (Symbol)

    a method name which is used for rendering

  • args

    arguments passed to rendering method

Yields:

  • block passed to rendering method



186
187
188
189
# File 'lib/hammer_builder/abstract.rb', line 186

def render(object, method, *args, &block)
  object.__send__ method, self, *args, &block
  self
end

#resetObject

resets the builder to the state after creation - much faster then creating a new one



123
124
125
126
127
128
# File 'lib/hammer_builder/abstract.rb', line 123

def reset
  flush
  @_output.clear
  @_stack.clear
  self
end

#set_variables(instance_variables) { ... } ⇒ Object

sets instance variables when block is yielded

Parameters:

  • instance_variables (Hash{String => Object})

    hash of names and values to set

Yields:

  • block when variables are set, variables are cleaned up afterwards



159
160
161
162
163
164
# File 'lib/hammer_builder/abstract.rb', line 159

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

#text(text) ⇒ Object

escapes text to output



92
93
94
95
# File 'lib/hammer_builder/abstract.rb', line 92

def text(text)
  flush
  @_output << CGI.escapeHTML(text.to_s)
end

#to_htmlString

Returns output.

Returns:

  • (String)

    output



167
168
169
170
# File 'lib/hammer_builder/abstract.rb', line 167

def to_html()
  flush
  @_output.clone
end